一些基础排序算法

本文记录一些基础排序算法的代码,方便复习用,日后再作排序算法的总结

Shell Sort

/**
 *    Shell Sort using Hibbard increments: 1, 3, 5...
 */

void shell_sort(int a[], int n)
{
    // get hibbard increments
    int cnt = 0, r = n / 2;
    while(r)
    {
        r /= 2;
        cnt ++;
    }
    int *gaps = new int[cnt];
    for(int i = 0; i < cnt; ++ i)
        gaps[i] = pow(2, i + 1) - 1;

    for(int k = cnt - 1; k >= 0; -- k)
    {
        int gap = gaps[k];
        // an insertion sort
        for(int i = gaps[k]; i < n; ++ i)
        {
            int temp = a[i], j = i;
            for(; j >= gap && temp < a[j-gap]; j -= gap)
                a[j] = a[j-gap];
            a[j] = temp;
        }
    }
}

Heap Sort

/**
 *    Heap sort using Max Heap
 */

void percolate_down(int a[], int hole, int n)
{
    int temp = a[hole], child;

    // 2 * hole + 1, the left child of hole 
    for(child = 2 * hole + 1; child < n; child = 2 * hole + 1)
    {
        if(child + 1 < n && a[child] < a[child + 1])
            child ++;
        if(a[child] < temp)
            break;
        a[hole] = a[child];
        hole = child;
    }
    a[hole] = temp;
}

void heap_sort(int a[], int n)
{
    /* Build Heap */
    for(int i = (n - 2) / 2; i >= 0; -- i)
        percolate_down(a, i, n);

    /* Delete Max */
    for(int i = n - 1; i > 0; -- i)
    {
        swap(a[0], a[i]);
        percolate_down(a, 0, i); // becareful, the size have -1
    }
}

Merge Sort

void merge(int a[], int t[], int lPos, int rPos, int rEnd)
{
    int lEnd = rPos - 1;
    int tPos = lPos;
    int num = rEnd - lPos + 1;

    while(lPos <= lEnd && rPos <= rEnd)
        if(a[lPos] < a[rPos])
            t[tPos ++] = a[lPos ++];
        else
            t[tPos ++] = a[rPos ++];

    while(lPos <= lEnd)
        t[tPos ++] = a[lPos ++];
    while(rPos <= rEnd)
        t[tPos ++] = a[rPos ++];

    for(int i = 0; i < num; i ++, rEnd --)
        a[rEnd] = t[rEnd];
}

void merge_sort(int a[], int t[], int left, int right)
{
    if(left >= right) return;

    int center = (left + right) / 2;
    merge_sort(a, t, left, center);
    merge_sort(a, t, center + 1, right);
    merge(a, t, left, center + 1, right);
}

void merge_sort(int a[], int n)
{
    int *t = new int[n];
    merge_sort(a, t, 0, n - 1);
}

Quick sort

int partition1(int a[], int low, int high)
{
    int pivotpos = low;
    int temp = a[pivotpos]; //  用来作比较
    while(low < high)
    {
        while(low < high && a[high] > temp) -- high;
        a[low] = a[high];
        while(low < high && a[low] < temp) ++ low;
        a[high] = a[low];
    }
    a[low] = temp;
    return low;
}

void quick_sort(int a[], int low, int high)
{
    if(low >= high) return;

    int pivotloc = partition(a, low, high);
    quick_sort(a, low, pivotloc-1);
    quick_sort(a, pivotloc+1, high);
}

void quick_sort(int a[], int n)
{
    quick_sort(a, 0, n-1);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.