Data structure: common sorting algorithm (eight): merge sort (C++ implementation)

Data structure: common sorting algorithm (8): merge sort

(1) Basic idea:

Merge (Merge) sorting method is to merge two (or more) ordered lists into a new ordered list, that is, divide the sequence to be sorted into several sub-sequences, and each sub-sequence is ordered. Then the ordered subsequences are merged into the overall ordered sequence. In the second step of merge sort, the rules for sorting two ordered arrays are very simple. At the same time, compare the size of the first position of the two arrays, put the smaller one in an empty array, and then put the position in the empty array The pointer moves one back, and then continues to compare with the previous position of another array, and so on. Until any one of the arrays is popped out of the stack first, all the elements in the other array are appended to the new array.

​ Merge sort and quick sort have the same effect. Quick sort: first roughly sort the array into two sub-arrays, then recursively divide it into two sub-arrays until there is only one element in the sub-array, then it will be sorted naturally It can be summarized as sorting first and then recursive; merge sorting: No matter what, the array is divided into two sub-arrays, and the array is divided into two sub-arrays recursively, until there is only one element in the array, then sorting starts. Let the two arrays be sorted, and then sort the two arrays according to the recursive return. At the end, the entire array can be sorted.

(2) Examples

Example: a[15] = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48} Sort by merge sort

img

2.1 Iterative method

① Apply for space to make the size the sum of two sorted sequences. This space is used to store the merged sequence

② Set two pointers, the initial positions are the starting positions of the two sorted sequences respectively

③ Compare the elements pointed to by the two pointers, select a relatively small element and put it into the merge space, and move the pointer to the next position

④ Repeat step ③ until a pointer reaches the end of the sequence

⑤ Copy all remaining elements of another sequence directly to the end of the merged sequence

2.2 Recursion

① Merge every two adjacent numbers in the sequence to form floor(n/2) sequences. After sorting, each sequence contains two elements

② Merge the above sequences again to form floor(n/4) sequences, each sequence contains four elements

③ Repeat step ② until all elements are sorted

Example code:

// 归并排序(递归法)
template<typename T>
void merge_sort_recursive(T arr[], T reg[], int start, int end) {
    if (start >= end)
        return;
    int len = end - start, mid = (len >> 1) + start;
    int start1 = start, end1 = mid;
    int start2 = mid + 1, end2 = end;
    merge_sort_recursive(arr, reg, start1, end1);
    merge_sort_recursive(arr, reg, start2, end2);
    int k = start;
    while (start1 <= end1 && start2 <= end2)
        reg[k++] = arr[start1] < arr[start2] ? arr[start1++] : arr[start2++];
    while (start1 <= end1)
        reg[k++] = arr[start1++];
    while (start2 <= end2)
        reg[k++] = arr[start2++];
    for (k = start; k <= end; k++)
        arr[k] = reg[k];
}

// merge_sort
template<typename T>
void merge_sort(T arr[], const int len) {
    T reg[15];
    merge_sort_recursive(arr, reg, 0, len - 1);
}

3. Summary:

Time complexity: Merge sorting is mainly divided into splitting and sorting ordered arrays. The time complexity of the split operation is logn, and the complexity of sorting is n, so the time complexity of merge sorting is O(nlogn) merge sort The space complexity of is the space occupied by the temporary array and the data pushed onto the stack during recursion: n + logn, so the space complexity is O(n)

Guess you like

Origin blog.csdn.net/qq_43801020/article/details/108219886