"merge sort" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "merge sort" answered properly. Developers are finding an appropriate answer about merge sort related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like merge sort. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on merge sort. 

merge sort

By ujjwal sotraujjwal sotra on May 27, 2021
//merge sort
#include <iostream>

using namespace std;
void merge(int arr[],int start,int mid,int end)
{
    int n1=mid-start+1;
    int n2=end-mid;
    int l[n1],m[n2];
    for(int i=0;i<n1;i++)
    {
        l[i]=arr[start+i];
    }
    for(int j=0;j<n2;j++)
    {
        m[j]=arr[mid+1+j];
    }
    int i=0;
    int j=0;
    int k=start;
    while(i<n1&&j<n2)
    {
        if(l[i]<m[j])
        {
            arr[k]=l[i];
            k++;
            i++;
        }
        else
        {
            arr[k]=m[j];
            k++;
            j++;
        }
    }
    while(i<n1)
    {
        arr[k]=l[i];
        k++;
        i++;
    }
    while(j<n2)
    {
        arr[k]=m[j];
        k++;
        j++;
    }
}
void mergesort(int arr[],int start,int end)
{
    if(start<end)
    {
        int mid=(start+end)/2;
        mergesort(arr,start,mid);
        mergesort(arr,mid+1,end);
        merge(arr,start,mid,end);
    }
}
void display(int arr[],int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

int main()
{
    int n;
    cout<<"enter the size of the array:"<<endl;
    cin>>n;
    cout<<"enter the elements of the array:"<<endl;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    cout<<"array as it is:"<<endl;
    display(arr,n);
    cout<<"sorted array:"<<endl;
    mergesort(arr,0,n-1);
    display(arr,n);
    return 0;
}

Add Comment

4

Merge sort

By Itchy ImpalaItchy Impala on May 31, 2021
class Sort 
{
    void merge(int arr[], int left, int middle, int right)
    {
        int low = middle - left + 1;                    //size of the left subarray
        int high = right - middle;                      //size of the right subarray
 
        int L[] = new int[low];                             //create the left and right subarray
        int R[] = new int[high];

        int i = 0, j = 0;
 
        for (i = 0; i < low; i++)                               //copy elements into left subarray
        {
            L[i] = arr[left + i];
        }
        for (j = 0; j < high; j++)                              //copy elements into right subarray
        {
            R[j] = arr[middle + 1 + j];
        }
        
 
        int k = left;                                           //get starting index for sort
        i = 0;                                             //reset loop variables before performing merge
        j = 0;

        while (i < low && j < high)                     //merge the left and right subarrays
        {
            if (L[i] <= R[j]) 
            {
                arr[k] = L[i];
                i++;
            }
            else 
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
 
        while (i < low)                             //merge the remaining elements from the left subarray
        {
            arr[k] = L[i];
            i++;
            k++;
        }
 
        while (j < high)                           //merge the remaining elements from right subarray
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
 

    void mergeSort(int arr[], int left, int right)       //helper function that creates the sub cases for sorting
    {
        int middle;
        if (left < right) {                             //sort only if the left index is lesser than the right index (meaning that sorting is done)
            middle = (left + right) / 2;
 
            mergeSort(arr, left, middle);                    //left subarray
            mergeSort(arr, middle + 1, right);               //right subarray
 
            merge(arr, left, middle, right);                //merge the two subarrays
        }
    }
 
    void display(int arr[])                 //display the array
    {  
        for (int i=0; i<arr.length; ++i) 
        {
            System.out.print(arr[i]+" ");
        } 
    } 

    public static void main(String args[])
    {
        int arr[] = { 9, 3, 1, 5, 13, 12 };
        Sort ob = new Sort();
        ob.mergeSort(arr, 0, arr.length - 1);
        ob.display(arr);
    }
}

Source: favtutor.com

Add Comment

1

merge sort

By adriancmirandaadriancmiranda on May 31, 2020
// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

function merge(list, start, midpoint, end) {
    const left = list.slice(start, midpoint);
    const right = list.slice(midpoint, end);
    for (let topLeft = 0, topRight = 0, i = start; i < end; i += 1) {
        if (topLeft >= left.length) {
            list[i] = right[topRight++];
        } else if (topRight >= right.length) {
            list[i] = left[topLeft++];
        } else if (left[topLeft] < right[topRight]) {
            list[i] = left[topLeft++];
        } else {
            list[i] = right[topRight++];
        }
    }
}

function mergesort(list, start = 0, end = undefined) {
    if (end === undefined) {
        end = list.length;
    }
    if (end - start > 1) {
        const midpoint = ((end + start) / 2) >> 0;
        mergesort(list, start, midpoint);
        mergesort(list, midpoint, end);
        merge(list, start, midpoint, end);
    }
    return list;
}

mergesort([4, 7, 2, 6, 4, 1, 8, 3]);

Add Comment

2

merge sort

By Outstanding OysterOutstanding Oyster on Jan 16, 2021
# Python3 recursive merge sort algorithm -> O(n*log(n))
def merge_sort(A):
    def merge(l, r):
        i = j = 0
        n = []  # merging container
        while i < len(l) or j < len(r):

            # if no more elements to the right,
            # add remaining left elements
            if i == len(l):
                n.extend(r[j:])
                break

            # if no more elements to the left,
            # add remaining right elements
            if j == len(r):
                n.extend(l[i:])
                break

            # if elements left on both sides,
            # add smaller element
            a, b = l[i], r[j]
            if a < b:
                n.append(a)
                i += 1
            else:
                n.append(b)
                j += 1

        return n

    # divide list down to single-elements
    s = len(A)
    if s > 1:
        s //= 2
        l = merge_sort(A[:s])  # split left
        r = merge_sort(A[s:])  # split right
        return merge(l, r)  # merge sides in order
    else:
        return A

Add Comment

0

merge sort

By Ashamed AlligatorAshamed Alligator on Jun 17, 2021
Given array is 
12 11 13 5 6 7 
Sorted array is 
5 6 7 11 12 13

Source: www.geeksforgeeks.org

Add Comment

0

merge sort

By Successful SwanSuccessful Swan on May 03, 2021
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.

Source: www.tutorialspoint.com

Add Comment

0

All those coders who are working on the C++ based application and are stuck on merge sort can get a collection of related answers to their query. Programmers need to enter their query on merge sort related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about merge sort for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of merge sort while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "merge sort". Visit this developer's friendly online web community, CodeProZone, and get your queries like merge sort resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "merge sort"

View All C++ queries

C++ queries related to "merge sort"

merge sort merge sort in c++ merge sort code in c++ merge sort c++ vector merge sort c++ github merge sort in c sort char array c++ using insertion sort Write a program to sort an array 100,200,20, 75,89.198, 345,56,34,35 using Bubble Sort. The program should be able to display total number of passes used for sorted data in given data set. sort char array c++ using insertion sort descending order how to merge string array in C++ merge images opencv c++ merge vector c++ how to sort a vector in reverse c++ how to sort an array c++ how to sort in descending order c++ how to sort a vector in c++ vector sort in reverse order c++ sort in descending order c++ stl how to sort a string in c++ sort a string alphabetically c++ bucket sort algorithm c++ simple -vector reverse sort cpp bubble sort in c++ c++ how to sort numbers in ascending order binary sort c++ how to sort vector in c++ c++ sort function time complexity sort vector struct c++ how to sort an array in c++ c++ sort array of ints define my own compare function sort C++ stl how to sort in descending order in c++ sort vector descending sort a vector of strings according to their length c++ sort string vector of words alphabetically c++ . Shell sort in c++ vector sort c++ The number of swaps required in selection sort stl sort in c++ how to make a selection sort C++ sort vector in descending order c++ sort std vector sort what is time complexity of insertion sort Heap sort in c++ array sort c++ insertion sort in c++ program sort function in cpp sort vector c++ quick sort in c++ how to sort array in c++ bubble sort c++ template Radix Sort in c++ quick sort predefined function in c++ c++ set sort order code for bubble sort in c++ c++ sort topological sort cp algorithms sort inbuilt function in c++ sort vector of strings c++ stl sort insertion sort in c++ sort a vector c++ sort vector of pairs c++ heap sort heapify and max heap in binary tree sort tuple c++ turbo sort codechef solution c++ buble sort sort strings by length and by alphabet sort n characters in descending order c++ sort using comparator anonymous function c++ how to sort string containing numbers in c++ Sort by the distance between pairs c++ c++ bubble sort heap sort internal implementation using c++ extra parameter in comparator function for sort write a c++ program that reads ten strings and store them in array of strings, sort them and finally print the sorted strings sort using lambda c++ sort vector in descending order c++ sort in descending order c++ how to sort a vector bubble sort program in c++ sort in c++ sort c++ c++ sort vector of objects by property mergge sort c++ sort function sort vector topological sort Bubble Sort C++ c++ sort vector of objects by property.

Browse Other Code Languages

CodeProZone