Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <queue>
#include <algorithm>
#include <functional>
using namespace std;

void insertSort(int A[], int cnt, int step = 1)
{
    if (cnt < 2) return;

    int currInx = 0, currTgtInx = step;
    while (currTgtInx < cnt)
    {
        int i = currTgtInx;
        while (i > 0 && A[i] < A[i - step])
        {
            std::swap(A[i], A[i - step]);
            i -= step;
        }
        currInx ++;
        currTgtInx ++;
    }
};

void shellSort(int A[], int cnt)
{
    if (cnt < 2) return;
    insertSort(A, cnt, 5);
    insertSort(A, cnt, 3);
    insertSort(A, cnt, 1);
};

void selectionSort(int A[], int cnt)
{
    if (cnt < 2) return;

    for (int inx = 0; inx < cnt; inx ++)
    {
        int *p = std::min_element(A + inx, A + cnt);
        std::swap(A[inx], *p);
    }
}

void bubbleSort(int A[], int cnt)
{
    if (cnt < 2) return;

    for (int i = 0; i < cnt - 1; i ++)
    for (int j = i + 1; j > 0; j --)
    {
        if (A[j - 1] > A[j])
            std::swap(A[j - 1], A[j]);
    }
};

void mergeSort(int A[], int i, int j)
{
    if (i == j) return;

    int mid = i + ((j - i) >> 1);
    mergeSort(A, i, mid);
    mergeSort(A, mid + 1, j);

    //    merge
    std::sort(A + i, A + j); // i know
};

void heapSort(int A[], int cnt)
{
    priority_queue<int, std::vector<int>, std::greater<int>> q;
    for (int i = 0; i < cnt; i ++)
        q.push(A[i]);

    int inx = 0;
    while (!q.empty())
    {
        A[inx++] = q.top();
        q.pop();
    }
};

void radixSort(int A[], int cnt)
{
    vector<vector<int>> bucket; 
    bucket.resize(10);

    for (int i = 0; i < cnt; i++)
        bucket[A[i] / 10].push_back(A[i]);
    
    for (vector<int> &v : bucket)
        if (!v.empty())
            std::sort(v.begin(), v.end());

    int inx = 0;
    for (vector<int> &v : bucket)
        if (!v.empty())
        {
            std::copy(v.begin(), v.end(), A + inx);
            inx += v.size();
        }
};

void quickSort(int A[], int i, int j)
{
    //    base case
    if (i == j) return;

    //    partition
    int mid = i + ((j - i) >> 1);
    
    int pivot = A[mid];
    std::swap(A[mid], A[j]);

    int tgtInx = i;
    for (int k = i; k < j; k++)
    {
        if (A[k] < pivot)
        {
            swap(A[tgtInx], A[k]);
            tgtInx ++;
        }
    }
    std::swap(A[tgtInx], A[j]);

    //    recursion
    quickSort(A, i, mid);
    quickSort(A, mid + 1, j);
};

//////////
void printArray(int A[], int cnt)
{
    for (int i = 0; i < cnt; i++)    printf("%d ", A[i]);
    printf("\n");
};
//////////

int main() {

    int in[] = {2,5,6,8,2,6,87,88,52,3,99,84,22,58,54};
    int cnt = sizeof(in) / sizeof(int);

    //insertSort(in, cnt);
    //shellSort(in, cnt);
    //selectionSort(in, cnt);
    //bubbleSort(in, cnt);
    //mergeSort(in, 0, cnt - 1);
    //heapSort(in, cnt);
    //radixSort(in, cnt);
    quickSort(in, 0, cnt - 1);

    printArray(in, cnt);
    
    system("pause");
    return 0;
}

 And qsort in Haskell:

qsort :: [Integer] -> [Integer]
qsort [] = []
qsort (x:xs) = qsort (filter (<x) xs) ++ [x] ++ qsort (filter (>= x) xs)

main = print $ qsort [13,43,5,23,56,76,12,43]
posted on 2014-11-03 16:07  Tonix  阅读(141)  评论(0)    收藏  举报