Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,221 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,129 people online right now. Registration is fast and FREE... Join Now!





Merge Sort

Merge sorting is recursive; it divides the array in half, sorts it and then repeats.

Submitted By: KYA
Actions:
Rating:
Views: 1,548

Language: C++

Last Modified: June 9, 2008
Instructions: You would use this in some program where fast log(n) efficiency is needed in a sort. (In comparison, bubble sort at worst is log (n^2))

Snippet


  1. /* Merge sort snippet
  2. * main() added to show how it works
  3. * you need to merge sort
  4. * Not guaranteed log(n) efficiency, but it is better then other
  5. * sorting methods
  6. * You sort one array,
  7. * The temp array is used to do this
  8. * Memory requirements are higher then other sort methods
  9. * due to the temp array
  10. */
  11.  
  12. /*
  13. * KYA
  14. * Merge Sort
  15. * 6-3-2008
  16. */
  17.  
  18. #include <iostream>
  19. using namespace std;
  20.  
  21. //Function Declarations
  22. void mergeSort(int numbers[], int temp[], int array_size);
  23. void m_sort(int numbers[], int temp[], int left, int right);
  24. void merge(int numbers[], int temp[], int left, int mid, int right);
  25.  
  26. int main()
  27. {
  28.         int arrayOne[5] = {65, 72, 105, 55, 2};
  29.         int arrayTwo[5];
  30.  
  31.         mergeSort(arrayOne, arrayTwo, 5);
  32.  
  33.         for (int i = 0; i < 5; i++)
  34.         {
  35.                 cout << arrayOne[i] << " ";
  36.         }//end for
  37.  
  38.         return 0;
  39. }//end main
  40.  
  41.  
  42. // "Main" function of the sequence
  43. // From here on out everything is called recursively
  44. void mergeSort(int numbers[], int temp[], int array_size)
  45. {
  46.   m_sort(numbers, temp, 0, array_size - 1);
  47. }
  48.  
  49.  
  50. void m_sort(int numbers[], int temp[], int left, int right)
  51. {
  52.   int mid;
  53.  
  54.   if (right > left)
  55.   {
  56.     mid = (right + left) / 2;
  57.     m_sort(numbers, temp, left, mid);
  58.     m_sort(numbers, temp, (mid+1), right);
  59.  
  60.     merge(numbers, temp, left, (mid+1), right);
  61.   }
  62. }
  63.  
  64. void merge(int numbers[], int temp[], int left, int mid, int right)
  65. {
  66.   int i, left_end, num_elements, tmp_pos;
  67.  
  68.   left_end = (mid - 1);
  69.   tmp_pos = left;
  70.   num_elements = (right - left + 1);
  71.  
  72.   while ((left <= left_end) && (mid <= right))
  73.   {
  74.     if (numbers[left] <= numbers[mid])
  75.     {
  76.       temp[tmp_pos] = numbers[left];
  77.       tmp_pos += 1;
  78.       left += 1;
  79.     }
  80.     else
  81.     {
  82.       temp[tmp_pos] = numbers[mid];
  83.       tmp_pos += 1;
  84.       mid += 1;
  85.     }
  86.   }
  87.  
  88.   while (left <= left_end)
  89.   {
  90.     temp[tmp_pos] = numbers[left];
  91.     left += 1;
  92.     tmp_pos += 1;
  93.   }
  94.   while (mid <= right)
  95.   {
  96.     temp[tmp_pos] = numbers[mid];
  97.     mid += 1;
  98.     tmp_pos += 1;
  99.   }
  100.  
  101.   for (i=0; i <= num_elements; i++)
  102.   {
  103.     numbers[right] = temp[right];
  104.     right -= 1;
  105.   }
  106. }
  107.  
  108. //Take out the C++ features (in main)  and this can be used as pure C code as well
  109.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month