program to implement merge sort in c 2b 2b

Solutions on MaxInterview for program to implement merge sort in c 2b 2b by the best coders in the world

showing results for - "program to implement merge sort in c 2b 2b"
Sol
16 Oct 2020
1#include <iostream>
2using namespace std;
3 
4
5void merge(int arr[], int l, int m, int r)
6{
7    int n1 = m - l + 1;
8    int n2 = r - m;
9 
10 
11    int L[n1], R[n2];
12 
13   
14    for (int i = 0; i < n1; i++)
15        L[i] = arr[l + i];
16    for (int j = 0; j < n2; j++)
17        R[j] = arr[m + 1 + j];
18
19 
20    int i = 0;
21 
22    
23    int j = 0;
24 
25    
26    int k = l;
27 
28    while (i < n1 && j < n2) {
29        if (L[i] <= R[j]) {
30            arr[k] = L[i];
31            i++;
32        }
33        else {
34            arr[k] = R[j];
35            j++;
36        }
37        k++;
38    }
39 
40  
41    while (i < n1) {
42        arr[k] = L[i];
43        i++;
44        k++;
45    }
46 
47   
48    while (j < n2) {
49        arr[k] = R[j];
50        j++;
51        k++;
52    }
53}
54 
55
56void mergeSort(int arr[],int l,int r){
57    if(l>=r){
58        return;
59    }
60    int m = (l+r-1)/2;
61    mergeSort(arr,l,m);
62    mergeSort(arr,m+1,r);
63    merge(arr,l,m,r);
64}
65 
66
67void printArray(int A[], int size)
68{
69    for (int i = 0; i < size; i++)
70        cout << A[i] << " ";
71}
72 
73
74int main()
75{
76    int arr[] = { 12, 11, 13, 5, 6, 7 };
77    int arr_size = sizeof(arr) / sizeof(arr[0]);
78 
79    cout << "Given array is \n";
80    printArray(arr, arr_size);
81 
82    mergeSort(arr, 0, arr_size - 1);
83 
84    cout << "\nSorted array is \n";
85    printArray(arr, arr_size);
86    return 0;
87}
Roxane
16 Apr 2020
1#include<iostream>
2using namespace std;
3void swapping(int &a, int &b) {     //swap the content of a and b
4   int temp;
5   temp = a;
6   a = b;
7   b = temp;
8}
9void display(int *array, int size) {
10   for(int i = 0; i<size; i++)
11      cout << array[i] << " ";
12   cout << endl;
13}
14void merge(int *array, int l, int m, int r) {
15   int i, j, k, nl, nr;
16   //size of left and right sub-arrays
17   nl = m-l+1; nr = r-m;
18   int larr[nl], rarr[nr];
19   //fill left and right sub-arrays
20   for(i = 0; i<nl; i++)
21      larr[i] = array[l+i];
22   for(j = 0; j<nr; j++)
23      rarr[j] = array[m+1+j];
24   i = 0; j = 0; k = l;
25   //marge temp arrays to real array
26   while(i < nl && j<nr) {
27      if(larr[i] <= rarr[j]) {
28         array[k] = larr[i];
29         i++;
30      }else{
31         array[k] = rarr[j];
32         j++;
33      }
34      k++;
35   }
36   while(i<nl) {       //extra element in left array
37      array[k] = larr[i];
38      i++; k++;
39   }
40   while(j<nr) {     //extra element in right array
41      array[k] = rarr[j];
42      j++; k++;
43   }
44}
45void mergeSort(int *array, int l, int r) {
46   int m;
47   if(l < r) {
48      int m = l+(r-l)/2;
49      // Sort first and second arrays
50      mergeSort(array, l, m);
51      mergeSort(array, m+1, r);
52      merge(array, l, m, r);
53   }
54}
55int main() {
56   int n;
57   cout << "Enter the number of elements: ";
58   cin >> n;
59   int arr[n];     //create an array with given number of elements
60   cout << "Enter elements:" << endl;
61   for(int i = 0; i<n; i++) {
62      cin >> arr[i];
63   }
64   cout << "Array before Sorting: ";
65   display(arr, n);
66   mergeSort(arr, 0, n-1);     //(n-1) for last index
67   cout << "Array after Sorting: ";
68   display(arr, n);
69}
queries leading to this page
merge sort code in c 2b 2bimplement the merge sort algorithm c 2b 2bimplement merge sort showing output step by step in c 2b 2b simple programint getsize 28 29 in c divide function merge sortmerge sort function in c 2b 2bmerge sort stl cpppmerge sort recusicemerge sorting code in c 2b 2bmerge sort c 2b 2b 5cmerge sort c 2b 2b programmerge sort c 2b 2b stl implementationmergesort cppmerge sort in place c 2b 2bmerge sort program in cppmerge sort code c 2b 2b2 way merge sort code in cppmerge sort algorithm c 2b 2bmerge sort cp algorithmmerge sort cppmergesort code cppmerge sort algorithm in cpp merge sort c 2b 2bstep through merge sort cppmerge sorting in c 2b 2bc 2b 2b inbuilt merge sortmerge sort time complexityusing merge sort in c 2b 2bmerge sort c 2b 3dc 2b 2b code for merge sortmerge sort in c 2b 2b stlwrite a recursive algorithm for merge sortmerge sort in c 2b 2b analysismerge sort algorithm in c 2b 2bnatural merge sort algorithm in c 2b 2bmerge sort implementation using c 2b 2bmergesort in c 2b 2bsort mergemerge sort cpp programmerge sort implementation in c 2b 2bcpp merge sortedmerge sort in c 2b 2b for decreasing ordercontoh program merge sort c 2b 2bmerge sort program in c explanationmerge sort using stl c 2b 2bhow to implement merge sort algorithm in c 2b 2bmerge sort in ccmerge function merge sort c 2b 2bprint merge sort c 2b 2bmerge sort c 2b 2b 27merge sort c 2b 2b implementationmerge array c 2b 2bmerge and sort in c 2b 2bmerge sort in cmerge sort data structure c 2b 2bmerge sort c 2b 2bmerge sort stl cppmerge sort algorithm c 2b 2b arrayalgoritm merge sort c 2b 2bmerge sort program in c 2b 2bmerge sorting array c 2b 2bsorting data using mergesort in c 2b 2b cheggmerge sort code in c 2b 2bmerge sort in cpp forclrs merge sort c 2b 2bimplement merge sort in c 2b 2b2 way merge sort in cppc 2b 2b merge sort examplemerge sort algorithm time and space complexityc 2b 2b merge and sort arraysmerge sort implementation in cppmerge sort algorithms in c 2b 2bc 2b 2b merge sort 23includemergecom example c 2b 2bmergesort cpp codef we sort a sorted array using mergesort then it can be done in o 28nlogn 29 time mergesort c 2b 2bc 2b 2b array merge sortmerge sort algorithm c 2b 2bmerge sort on array implementation in c 2b 2bwhat is code of merge sort in c 2b 2bmergesort in cppmerge sort c 2b 2b stdcpp merge and sortcpp merge sorthow to perform merge sort in c 2b 2bprogram for implementing merge sort in cppmerge sort in c 2b 2bcp algorithms merge sortmerge sort cpp codemerge sort cpp implementationprogram to implement merge sort in c 2b 2bbest merge sort implementation c 2b 2bmerge sort code in alogoridom c 2b 2bmerge sort c 2b 2b examplemerge sort array c 2b 2b codemerge sort in cppcontoh merge sort ascending order c 2b 2bmerge sort in c 2bc 2b 2b mergesortc 2b 2b merge sort functionsort an array a using merge sort c 2b 2bmerge sort c 2b 2b stlmerge sort in c 2b 2b simple programprogramming merge sortc 2b 2b merge sort stlmerge sort code c 2b 2bmerge algorithm sort cppc 2b 2b merge sortmerge sort code which return sorted array in cmerge sort recursive c 2b 2bmerge sort algorithm in c 2b 2bmerge sort library c 2b 2bsorting data using mergesort in c 2b 2bmerge sort c 2b 2b codestd merge sort c 2b 2bmerge the partitions c 2b 2b cdemerge sorting program in c 2b 2bmerge sort merge functionmerge sort the array in c 2b 2beasy merge sort algorithm in c 2b 2bmergesort code c 2b 2bc 2b 2b merge sort methodmerge sort explained c 2b 2bmerge sort algorithm cppwhere does the sort happen in merge sort c 2b 2bprogram c 2b 2b merge sortmerge sort implementation c 2b 2b3 way merge sort c 2b 2bprogram to implement merge sort in c 2b 2b