C++11写算法之插入排序

 

插入排序,是指将从1 –> size-1的数一个个插入到前面已经排序好的数组中。

时间复杂度:O(n^2) , O(nlgn) (lgn指使用二分查找插入点位置)

空间复杂度:O(1)

// #if __cplusplus < 201103L 
// #error "must be compiled under c++11 support platform!!!"
// #endif
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cassert>
using namespace std;

//find insert position function
int FindInsertPos(int varList[], const int size, const int target)
{
    if (!varList || size < 0)
    {
        assert(false);
        return -1;
    }
    int insertPos = 0;
    for (int i = 0; i < size;i++)
    {
        if (target < varList[i])
        {
            insertPos = i;
            break;
        }
    }
    return insertPos;
}
//binary find insert position function
int BinaryFindInsertPos(int varList[], const int size,const int target)
{
    if (!varList || size < 0)
    {
        assert(false);
        return -1;
    }
    int insertPos = 0;
    int begin = 0;
    int end = size - 1;
    int mid = (begin + end) >> 1;
    while (begin < end)
    {
        if (target > varList[begin] && target <= varList[mid] && 1 == (mid - begin))
        {
            insertPos = mid;
            break;
        }
        else if (target > varList[mid] && target < varList[end] && 1 == (end - mid))
        {
            insertPos = end;
            break;
        }
        else if (target < varList[mid])
        {
            end = mid - 1;
        }
        else if (target > varList[mid])
        {
            begin = mid + 1;
        }
        mid = (begin + end) >> 1;
    }

    return insertPos;
}
//insert sort function
void InsertSort(int varList[], const int size)
{
    if (!varList || size <= 1)
    {
        return;
    }
    auto findInsertPosFunc = BinaryFindInsertPos; //FindInsertPos
    for (int i = 1; i < size; i++)
    {
        if (varList[i - 1] > varList[i])
        {
            int tmp = varList[i];
            int insertPos = findInsertPosFunc(varList, i, varList[i]);
            //move
            for (int j = i; j > insertPos;j--)
            {
                varList[j] = varList[j - 1];
            }
            varList[insertPos] = tmp;
        }
    }
}

void test()
{
    //case counter
    int testCase = 0;
    //sort function object
    auto sortFunc = InsertSort;
    //show case result lambda function
    auto showFunc = [&testCase](const char* caseDescription){cout << "case[" << testCase++ << "]\t(" << caseDescription << ") \t\tok " << endl; };

    cout << "test begin : " << endl << endl;

    //case empty list
    {
        sortFunc(nullptr, 0);
        showFunc("case empty list");
    }
    //case wrong size
    {
        int nTestList[] = { 13, 52, 32, 15, 66, 2, 99, 202, 103, 2 };
        sortFunc(nTestList, 0);
        showFunc("case wrong size");
    }
    //case size == 1
    {
        int var = 13;
        int varList[] = { var };
        sortFunc(varList, 1);
        assert(var == varList[0]);
        showFunc("case size == 1");
    }
    //case normal sort
    {
        int varList[] = { 13, 52, 32, 15, 66, 2, 99, 202, 103, 2 };
        const int size = sizeof(varList) / sizeof(int);
        const int resultList[] = { 2, 2, 13, 15, 32, 52, 66, 99, 103, 202 };
        static_assert(sizeof(varList) == sizeof(resultList), "size of varList is not equal with resultList!!");

        sortFunc(varList, size);
        for (int i = 0; i < size; i++){ assert(varList[i] == resultList[i]); }
        showFunc("case normal sort");
    }
    //case sorted list
    {
        int varList[] = { 2, 2, 13, 15, 32, 52, 66, 99, 103, 202 };
        const int size = sizeof(varList) / sizeof(int);
        const int resultList[] = { 2, 2, 13, 15, 32, 52, 66, 99, 103, 202 };
        static_assert(sizeof(varList) == sizeof(resultList), "size of varList is not equal with resultList!!");

        sortFunc(varList, size);
        for (int i = 0; i < size; i++){ assert(varList[i] == resultList[i]); }
        showFunc("case sorted list");
    }
    cout << endl << "test done ! " << endl << endl;
}
int main(int argc, char* argv[])
{
    test();
    return 0;
}
posted @ 2014-04-23 00:35  *神气*  阅读(417)  评论(0编辑  收藏  举报