【排序】归并排序与快速排序:C++递归实现

实现功能

  1. 递归实现归并排序
  2. 递归实现快速排序

核心代码

/********************************************
 *  Author        : Dong Huaan
 *  Email         : donghuaan@163.com
 *  Filename      : 11.0_SortAndSearch.cpp
 *  Creation time : 2018-01-06 23:24:49
 *  Last modified : 2018-01-07 17:18:44
 *  Description   : ---
 *******************************************/

#include<iostream>

//***********************merge sort************************
// TOC: average: O(nlog(n)), worst: O(nlog(n))
// SOC: O(nlog(n))

void mergeSort(int *array, const int length);
void mergeSort(int *array, int low, int high, const int length);
void merge(int *array, int low, int middle, int high, const int length);

// merge two sub arrays;
void merge(int *array, int low, int middle, int high, const int length)
{
    // create one temp array to buffering;
    int temp[length] = { 0 };

    for(int index = low; index <= middle; ++index)
        temp[index] = array[index];

    int leftHelper = low;
    int rightHelper = middle + 1;
    int current = low;

    // append elements to array in ascending sort;
    while(leftHelper <= middle && rightHelper <= high)
        if(temp[leftHelper] < array[rightHelper])
            array[current++] = temp[leftHelper++];
        else
            array[current++] = array[rightHelper++];

    // add remaing elements int temp to array;
    for(; leftHelper <= middle;)
        array[current++] = temp[leftHelper++];
}

// overloading merge sort interface function
void mergeSort(int *array, int low, int high, const int length)
{
    if(low < high)
    {
        int middle = (low + high) / 2;
        mergeSort(array, low, middle, length);
        mergeSort(array, middle + 1, high, length);
        merge(array, low, middle, high, length);
    }
}

// merge sort interface function;
void mergeSort(int *array, const int length)
{
    if(!array || length < 1) return;

    mergeSort(array, 0, length - 1, length);
}

//*****************************quick sort*********************
// TOC: average: O(nlog(n)), worst: O(n^2)
// SOC: O(log(n))

// partition
int partition(int *array, int low, int high)
{
    int pivot = array[(low + high) / 2];

    while(low <= high)
    {
        while(array[low++] < pivot);
        while(array[high--] > pivot);

        if(--low <= ++high)
            std::swap(array[low++], array[high--]);
    }

    return low;
}

// overloading quick sort interface function;
void quickSort(int *array, int low, int high)
{
    int splitIndex = partition(array, low, high);

    if(low < splitIndex - 1)
        quickSort(array, low, splitIndex - 1);
    if(splitIndex < high)
        quickSort(array, splitIndex, high);
}

// quick sort interface function;
void quickSort(int *array, const int length)
{
    if(!array || length < 1) return;

    quickSort(array, 0, length - 1);
}

测试

功能测试:进行了少量的功能测试。未进行大量用例测试。
边界值测试:未进行边界值测试。
特殊输入测试:未进行特殊输入测试。

测试代码

//**********test code**********
// print array
void print(const int * const array, const int length)
{
    if(!array || length < 1) return;

    for(int i = 0; i < length; ++i)
        if(i == length - 1)
            std::cout << array[i] << std::endl;
        else
            std::cout << array[i] << "  ";
}

void test_MergeSort()
{
    std::cout << "merge sort test:\n";

    int array[] = { 7, 6, 8, 9, 3, 4, 5, 1, 2, 0, 4, 20 };
    const int length = sizeof(array) / sizeof(int);

    // print origin array;
    std::cout << "origin array: ";
    print(array, length);

    // print length of array;
    std::cout << "      length: ";
    std::cout << length << std::endl;

    // merge sort;
    mergeSort(array, length);

    // print sorted array;
    std::cout << "sorted array: ";
    print(array, length);
}

void test_QuickSort()
{
    std::cout << "\nquick sort test:\n";

    int array[] = { 7, 6, 8, 9, 3, 4, 5, 1, 2, 0, 4, 20 };
    const int length = sizeof(array) / sizeof(int);

    // print origin array;
    std::cout << "origin array: ";
    print(array, length);

    // print length of array;
    std::cout << "      length: ";
    std::cout << length << std::endl;

    // quick sort
    quickSort(array, length);

    // print sorted array;
    std::cout << "sorted array: ";
    print(array, length);
}

int main()
{
    test_MergeSort();
    test_QuickSort();
}

测试结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值