Sorting Algorithm: Merge Sort (C, Java)

merge sort

The idea of ​​merge sort is to divide the array into two parts, sort them separately, and then merge them together. The merge method merges two sorted parts of an array into one.


top-down merge sort


Because the problem is divided into two sub-problems in half each time, and the algorithm complexity of this half-division is generally O(NlogN), the time complexity of the merge sort method is also O(NlogN).

The recursive operation of small arrays will be too frequent, you can switch to insertion sort to improve performance when the array is too small.

bottom-up merge sort

Merge those micro arrays first, then merge the resulting subarrays pairwise.

#include<stdio.h>
#include<stdlib.h>

#define min(a,b) a<b?a:b

char tmp[] = {};

/*string length*/
int length(char *in)
{
    int i=0;
    while(in[i] != '\0')
        i++;
    return i;
}
/* output string */
void showString(char *str)
{
    int N = length(str);
    int i=0;
    while(i<N)
    {
        printf("%c ",str[i]);
        i++;
    }
    printf("\n");
}
/* merge method */
void merge(char *str, int low, int mid, int height)
{
    int i = low, j = mid + 1;
    int k;
    for(k=low;k<=height;k++)
    {
        tmp[k] = str[k];
    }
    for(k=low;k<=height;k++)
    {
        if(i>mid)
            str[k] = tmp[j++];
        else if(j>height)
            str[k] = tmp[i++];
        else if(tmp[i] <= str[j])
            str[k] = tmp[i++];
        else str[k] = tmp[j++];
    }
    showString(str);
}
/* Merge sort from top to bottom */
void tdSort(char *str, int low, int height)
{
    if(height<=low)return;
    int mid = low + (height - low)/2;
    tdSort(str, low, mid);
    tdSort(str, mid+1, height);
    merge(str, low, mid, height);
}
/* Merge sort from bottom to top */
void buSort(char *str)
{
    int N = length(str);
    int sz, lo;
    for (sz = 1; sz <N; sz + = sz)
    {
        for (lo = 0; lo <N - sz; lo + = sz + sz)
        {
            merge (str, lo, lo + sz - 1, min (lo + sz + sz - 1, N - 1));
        }
    }
}
int main(int argc, char **argv)
{
    char str[] = "bcefad";
    
    showString(str);
    tdSort(str, 0, length(str)-1);
    // buSort (str);
    showString(str);
    
    return 1;
}

The following Java code is taken from: https://github.com/CyC2018/Interview-Notebook/

/**
 * Merge Sort
 * https://github.com/CyC2018/Interview-Notebook/
 */
public class MergeSort {
    
    private int[] aux;

    /**
     * Merge
     */
    private void merge(int[] a, int lo, int mid, int hi)
    {
        int i = lo, j = mid + 1;

        for (int k = lo; k <= hi; k++)
        {
            aux[k] = a[k]; // copy data to tmp array
        }

        for (int k = lo; k <= hi; k++)
        {
            if (i > mid) a[k] = aux[j++];
            else if (j > hi) a[k] = aux[i++];
            else if (aux[i]<=a[j]) a[k] = aux[i++]; // for stable
            else a[k] = aux[j++];
        }
    }
    /**
     * Top-down merge sort
     */
    public void tdsort(int[] a)
    {
        aux = new int[a.length];
        tdsort(a, 0, a.length - 1);
    }

    private void tdsort(int[] a, int lo, int hi)
    {
        aux = new int[a.length];
        if (hi <= lo) return;
        int mid = lo + (hi - lo) / 2;
        tdsort(a, lo, mid);
        tdsort(a, mid + 1, hi);
        merge(a, lo, mid, hi);
    }
    /**
     * Bottom up merge sort
     */
    public void busort(int[] a)
    {
        int N = a.length;
        aux = new int[N];
        for (int sz = 1; sz <N; sz + = sz)
        {
            for (int lo = 0; lo <N - sz; lo + = sz + sz)
            {
                merge (a, lo, lo + sz - 1, Math.min (lo + sz + sz - 1, N - 1));
            }
        }
    }
    public void show(int[] a)
    {
        int N = a.length;
        for(int i=0;i<N;i++)
        {
            System.out.print(a[i]+" ");
        }
        System.out.println();
    }
    static public void main(String[]args)
    {
        int a[] = {5,4,3,2,1};
        MergeSort ms = new MergeSort();
        ms.show(a);
        ms.tdsort(a);
        ms.show(a);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326495252&siteId=291194637