m2skills

merge sort c++

Oct 13th, 2017
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. // program to implement recursive merge sort algorithm in c++
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void displayList(int elementList[], int length){
  7.     for(int index = 0; index < length; index++){
  8.         cout<<elementList[index]<<" ";
  9.     }
  10.     cout<<endl;
  11. }
  12.  
  13. // function that sorts and merges the list
  14. static void Merge(int* data, int left, int middle, int right) {
  15.     int i, j, k;
  16.    
  17.     // storing the left and the right list length
  18.     int leftLength = middle - left + 1;
  19.     int rightLength = right - middle;
  20.    
  21.     //creating 2 dummy lists to hold the split lists
  22.     int* leftTemp = new int[leftLength];
  23.     int* rightTemp = new int[rightLength];
  24.  
  25.     for (i = 0; i < leftLength; i++)
  26.         leftTemp[i] = data[left + i];
  27.  
  28.     for (j = 0; j < rightLength; j++)
  29.         rightTemp[j] = data[middle + 1 + j];
  30.  
  31.     // sorting and merging the left and right lists by element wise comparison
  32.     i = 0;
  33.     j = 0;
  34.     k = left;
  35.  
  36.     while (i < leftLength && j < rightLength)
  37.     {
  38.         if (leftTemp[i] <= rightTemp[j])
  39.         {
  40.             data[k] = leftTemp[i];
  41.             i++;
  42.         }
  43.         else
  44.         {
  45.             data[k] = rightTemp[j];
  46.             j++;
  47.         }
  48.  
  49.         k++;
  50.     }
  51.  
  52.     while (i < leftLength)
  53.     {
  54.         data[k] = leftTemp[i];
  55.         i++;
  56.         k++;
  57.     }
  58.  
  59.     while (j < rightLength)
  60.     {
  61.         data[k] = rightTemp[j];
  62.         j++;
  63.         k++;
  64.     }
  65. }
  66.  
  67. int* MergeSortRecursive(int* data, int left, int right) {
  68.     if (left < right)
  69.     {
  70.         int middle = left + (right - left) / 2;
  71.  
  72.         MergeSortRecursive(data, left, middle);
  73.         MergeSortRecursive(data, middle + 1, right);
  74.         Merge(data, left, middle, right);
  75.     }
  76.     return data;
  77. }
  78.  
  79. int main(){
  80.  
  81.     int *sortedList;
  82.     cout<<"Program to implement Recursive Merge sort algorithm in C++"<<endl;
  83.     int length, element;
  84.     cout<<"Enter the number of Elements to be sorted : ";
  85.     cin>>length;
  86.     int myList[length];
  87.     cout<<"Enter the elements to be sorted : ";
  88.     for(int index = 0; index < length; index++){
  89.         cin>>element;
  90.         myList[index] = element;
  91.     }
  92.  
  93.     cout<<"\nThe list of elements before sorting is : "<<endl;;
  94.     displayList(myList, length);
  95.  
  96.     sortedList = MergeSortRecursive(myList, 0, length-1);
  97.  
  98.     cout<<"\nThe sorted list is : "<<endl;
  99.     displayList(sortedList, length);
  100. }
Add Comment
Please, Sign In to add comment