Sorting-Two-Way Merge Sorting

Keywords: less

Algorithmic idea: splitting an unordered sequence into a subsequence with only one keyword, and then merging two sequences until merging into one sequence

Time complexity analysis: a total of log2n sorting operations are needed, each sorting performs n merging operations, so the time complexity is O(nlog2n); time complexity is independent of the initial sequence, and the average and best and worst time complexity are O(nlog2n).

Spatial Complexity: The whole disordered sequence needs to be dumped, and the spatial complexity is O(n)

Code:

void Merge(int arr[],int left,int mid,int right)
{
    int i,k;
    int left_min = left;
    int left_max = mid;
    int right_min = mid+1;
    int right_max = right;
    int *b = (int *)malloc((right-left+1)*sizeof(int));     //Apply for auxiliary space, the size of which is the total number of vertices of two subsequences
    for(k=0;left_min<=left_max&&right_min<=right_max;k++)   //Each cycle saves the minimum median of two subsequences into b-space
    {
        if(arr[left_min]<arr[right_min])
            b[k] = arr[left_min++];
        else
            b[k] = arr[right_min++];
    }
    if(left_min<=left_max)      //If the right length is less than left, the left at the end of the comparison is left, and the remaining nodes are inserted into the b space.
    {
        for(i=left_min;i<=left_max;i++)
            b[k++] = arr[i];
    }
    if(right_min<=right_max)    //If the length of right is longer than left, the left at the end of the comparison is left, and the remaining nodes are inserted into b space.
    {
        for(i=right_min;i<=right_max;i++)
            b[k++] = arr[i];
    }
    for(i=0;i<right-left+1;i++) //In this case, b-space is the result of merging two ordered subsequences, which are copied to the corresponding position of the original array.
        arr[left+i] = b[i];
    free(b);                    //Release Node Space
}
void Merge_sort(int arr[],int left,int right)
{
    if(left<right)
    {
        int mid = (left+right)/2;
        Merge_sort(arr,left,mid);           //Continuously split the left sequence until each subsequence has only one element
        Merge_sort(arr,mid+1,right);        //Continuously split the right sequence until each subsequence has only one element
        Merge(arr,left,mid,right);          //Merging subsequences
    }
}

Complete code:

#include <stdio.h>
#include <stdlib.h>
void Merge(int arr[],int left,int mid,int right)
{
    int i,k;
    int left_min = left;
    int left_max = mid;
    int right_min = mid+1;
    int right_max = right;
    int *b = (int *)malloc((right-left+1)*sizeof(int));     //Apply for auxiliary space, the size of which is the total number of vertices of two subsequences
    for(k=0;left_min<=left_max&&right_min<=right_max;k++)   //Each cycle saves the minimum median of two subsequences into b-space
    {
        if(arr[left_min]<arr[right_min])
            b[k] = arr[left_min++];
        else
            b[k] = arr[right_min++];
    }
    if(left_min<=left_max)      //If the right length is less than left, the left at the end of the comparison is left, and the remaining nodes are inserted into the b space.
    {
        for(i=left_min;i<=left_max;i++)
            b[k++] = arr[i];
    }
    if(right_min<=right_max)    //If the length of right is longer than left, the left at the end of the comparison is left, and the remaining nodes are inserted into b space.
    {
        for(i=right_min;i<=right_max;i++)
            b[k++] = arr[i];
    }
    for(i=0;i<right-left+1;i++) //In this case, b-space is the result of merging two ordered subsequences, which are copied to the corresponding position of the original array.
        arr[left+i] = b[i];
    free(b);                    //Release Node Space
}
void Merge_sort(int arr[],int left,int right)
{
    if(left<right)
    {
        int mid = (left+right)/2;
        Merge_sort(arr,left,mid);           //Continuously split the left sequence until each subsequence has only one element
        Merge_sort(arr,mid+1,right);        //Continuously split the right sequence until each subsequence has only one element
        Merge(arr,left,mid,right);          //Merging subsequences
    }
}
int main()
{
    int i;
    int arr[] = {56,12,78,34,10,90,23,67,56,43};
    Merge_sort(arr,0,9);
    for(i=0;i<10;i++)
        printf("%d\t",arr[i]);
}

Posted by CaptianChaos on Mon, 07 Oct 2019 10:48:18 -0700