Implementation of the five-merge sort in vernacular classical algorithm series

Source: Internet
Author: User

Merge sort is an efficient sorting algorithm based on the merging operation. This algorithm is a very typical application of the partition method (Divide and Conquer).

First consider how to combine the two ordered series. This is very simple, as long as the first number from the comparison of two series, who is the first to take who, after taking the corresponding sequence of the deletion of this number. Then the comparison, if there are several columns empty, then directly the data of the other sequence can be taken out in turn.

[CPP]View Plaincopy
  1. Merge ordered Arrays a[] and b[] into c[]
  2. void Memeryarray (int a[], int n, int b[], int m, int c[])
  3. {
  4. int I, j, K;
  5. i = j = k = 0;
  6. While (i < n && J < m)
  7. {
  8. if (A[i] < b[j])
  9. c[k++] = a[i++];
  10. Else
  11. c[k++] = b[j++];
  12. }
  13. While (i < n)
  14. c[k++] = a[i++];
  15. While (J < m)
  16. c[k++] = b[j++];
  17. }

It can be seen that the efficiency of the combined ordered series is relatively high and can reach O (n).

Solve the above merge ordered series, and then look at the merger sort, the basic idea is to divide the array into two groups, a, if the two groups of data are ordered, then it is convenient to the two groups of data to sort. How do you get the data in the two groups in order?

A and B groups can be divided into two groups respectively. And so on, when the group has only one data, it can be thought that the group has reached an orderly, and then merge the adjacent two groups. This is done by first recursive decomposition of the sequence, and then the sequence is completed by the merge sequence.

[CPP]View Plaincopy
  1. There will be two ordered series A[first...mid] and A[mid ... Last] Merge.
  2. void Mergearray (int a[], int first, int mid, int last, int temp[])
  3. {
  4. int i = First, j = mid + 1;
  5. int m = Mid, n = last;
  6. int k = 0;
  7. While (i <= m && j <= N)
  8. {
  9. if (A[i] <= a[j])
  10. temp[k++] = a[i++];
  11. Else
  12. temp[k++] = a[j++];
  13. }
  14. While (i <= m)
  15. temp[k++] = a[i++];
  16. While (j <= N)
  17. temp[k++] = a[j++];
  18. For (i = 0; i < K; i++)
  19. A[first + i] = temp[i];
  20. }
  21. void MergeSort (int a[], int first, int last, int temp[])
  22. {
  23. if (first < last)
  24. {
  25. int mid = (first + last)/2;
  26. MergeSort (A, first, mid, temp); //Left order
  27. MergeSort (A, mid + 1, last, temp); //Right order
  28. Mergearray (A, first, mid, last, temp); //Merge two sequential series
  29. }
  30. }
  31. BOOL MergeSort (int a[], int n)
  32. {
  33. int *p = new int[n];
  34. if (p = = NULL)
  35. return false;
  36. MergeSort (A, 0, n-1, p);
  37. delete[] p;
  38. return true;
  39. }

The efficiency of the merge sort is relatively high, set the sequence length is N, divide the series into small series to Logn step, each step is a process of merging ordered series, the time complexity can be recorded as O (N), so altogether O (N*logn). Because the merge sort is done in the adjacent data each time, several sorting methods (quick sorting, merge sort, hill sort, heap sort) of the merge sort in O (N*logn) are also relatively efficient.

In My computer on the bubble sort, direct insertion sort, merge sort and directly use the system qsort () to compare (all in release version)

Test 20,000 random data:

Test 50,000 random data:

Then test the 200,000 random data:

Note: Some books allocate a temporary array when Mergearray () merges an ordered series, but excessive new operations can be very time consuming. So a little change has been made. Only a temporary array of new in MergeSort (). This temporary array is shared by all subsequent operations.

Implementation of the five-merge sort in vernacular classical algorithm series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.