Merge and sort. I'll give you an example and you'll understand it

Abstract: Merge Sort is an effective and stable sorting algorithm based on merge operation. It is a very typical application of Divide and Conquer.

This article is shared from Huawei cloud community< You can understand it at a glance! Graphic merging and sorting >, by bigsai.

Merge Sort is an effective and stable sorting algorithm based on merge operation. It is a very typical application of Divide and Conquer.

1, Algorithmic thought

The main idea of merging and sorting is divide and conquer. The main processes are:

1. Cut n elements from the middle and divide them into two parts.

2. Divide the remaining arrays recursively until the size of the array is 1. At this time, there is only one element, then the array is ordered.

3. Gradually merge two ordered series from the bottom. Merge two arrays of size 1 into an ordered array of size 2, and then merge two ordered arrays of size 2 into an ordered array of size 4... Until all small arrays are merged.

2, Thinking

So how to combine two ordered sequences into an ordered sequence?

Let's take a chestnut handle. We can understand it at a glance.

3, Take a chestnut

For example, there is array arr [3,7,8,10,2,4,6,9]; We can divide this array into two ordered subsequences.

They are [3,7,8,10] and [2,4,6,9], respectively, and they are combined into ordered sequences [2,3,4,6,7,8,9,10].

Step 1:

Split the two small arrays into left array and right array. As shown in the following figure, use i to point to the first element of left and j to point to the first element of right.

Step 2:

Create an empty array arr and use k to point to the first element of the array.

Step 3:

Compare the numbers indicated by i and j and place the small number in the position indicated by k. At the same time, move the position indicated by the small number and the position indicated by k one bit to the right.

2 < 3, fill 2 into the arr array and shift j and k to the right at the same time.

3 < 4, fill 3 into the arr array and shift i and k to the right at the same time.

4 < 7, fill 4 into the arr array and shift j and k to the right at the same time.

6 < 7, fill 6 into the arr array and shift j and k to the right at the same time.

7 < 9, fill 7 into the arr array and shift i and k to the right at the same time.

8 < 9, fill 8 into the arr array and shift i and k to the right at the same time.

10 > 9, fill 9 into the arr array and shift j and k to the right at the same time.

You can find that the right array has been filled in at this time, so you only need to fill the remaining numbers of the left array in arr at this time.

A meal is as fierce as a tiger. In this way, two ordered arrays are arranged in order by merging. Isn't it great.


So the question is, can merge sorting only arrange such an ordered array?

What if an unordered array appears? For example, this array now becomes arr [8,7,2,10,3,9,4,6];

4, Problem solving

At this moment, we need to use the divide and conquer strategy (divide and conquer divides the problem into some small problems and then solve them recursively, while the conquer stage "fixes" the answers obtained in different stages, that is, divide and conquer).

In fact, the third part above is the process of conquer, which combines two ordered sequences into an ordered sequence.

Chestnuts: diagrammatically disordered sequences for Hill sorting.

5, Algorithm implementation

       #include <stdio.h>
       void merge(int arr[], int L, int M, int R) {
         int LEFT_SIZE = M - L;
         int RIGHT_SIZE = R - M + 1;
         int left[LEFT_SIZE];
         int right[RIGHT_SIZE];
         int i, j, k;
         // Fill the left array
          for (i=L; i<M; i++){
            left[i-L] = arr[i];
          }
         // Fill the array on the right
          for (i=M; i<=R; i++){
            right[i-M] = arr[i];
          }
       // for (int i=0; i<LEFT_SIZE; i++){
       // printf("%d\n",left[i]);
       // }
       // 
       // for (int i=0; i<RIGHT_SIZE; i++){
       // printf("%d\n",right[i]);
       // }
         // Merge array
          i = 0; j = 0; k = L;
          while (i < LEFT_SIZE && j < RIGHT_SIZE){
            if (left[i] < right[j]){
              arr[k] = left[i];
              i++;
              k++;
            }else{
              arr[k] = right[j];
              j++;
              k++;
            }
          }
          while(i < LEFT_SIZE){
            arr[k] = left[i];
            i++;
            k++;
          }
          while(j < RIGHT_SIZE){
            arr[k] = right[j];
            j++;
            k++;
          }
       }
       void mergeSort(int arr[], int L, int R){
         if (L == R){
           return;
         }else{
           int M = (L + R) / 2;
           mergeSort(arr,L,M);
           mergeSort(arr, M+1,R);
           merge(arr, L, M+1,R);
         }
       }
       int main(){
       // int arr[] = {3,7,8,10,2,4,6,9};
         int arr[] = {8,7,2,10,3,9,4,6};
         int L = 0;
         int M = 4;
         int R = 7;
         mergeSort(arr,L,R);
         for (int i=0; i<=R; i++){
           printf("%d\n",arr[i]);
         }
       }

Output:

6, Algorithm analysis

Time complexity: O(nlogn).

Space complexity: O(N). Merge sorting requires an array of the same length as the original array as an auxiliary to sort.

Stability: stable, because the restriction of not moving can be made under the condition of equality when exchanging elements, so the merging sort can be stable.

7, Applicable scenario

Merge sort requires a temporary array with the same space as the array to be sorted. Therefore, when using merge sort, you need to consider whether there are space constraints. If there are no space constraints, merge sort is a good choice.

 

Click focus to learn about Huawei cloud's new technologies for the first time~

Posted on Sun, 28 Nov 2021 20:16:08 -0500 by adzie