【排序】归并排序与快速排序:C++递归实现
2018-01-07 21:23
627 查看
实现功能
递归实现归并排序递归实现快速排序
核心代码
/******************************************** * 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(); }
测试结果
相关文章推荐
- 【算法和数据结构】排序(四)归并排序和快速排序(C++实现)
- 排序算法的C++实现与性能分析(插入排序、归并排序、快速排序、STOOGE排序、堆排序)
- 常用排序算法C++实现(堆排序,快速排序,归并排序,基数排序)
- java中各种常用排序实现(直接插入排序、直接选择排序、堆排序、冒泡排序、快速排序和归并排序)
- 算法导论之插入排序,选择排序,归并排序,冒泡排序,希尔排序,堆排序,快速排序的c语言实现
- 归并排序 C++/Java 递归实现代码 -犯了菜鸟老毛病
- C++实现快速排序,递归写法
- python中实现二分查找,插入排序,归并排序,快速排序
- 快速排序、归并排序的递归、非递归实现
- 两种归并排序算法的实现:二路归并排序和基本归并排序(虚拟消除递归的二路归并排序)
- 算法导论之插入排序,选择排序,归并排序,冒泡排序,希尔排序,堆排序,快速排序的c语言实现
- 插入排序、冒泡排序、选择排序、希尔排序、快速排序、归并排序、堆排序和LST基数排序的C++代码实现
- 无聊写排序之 ---- 归并排序(MergeSort) 非递归实现
- C++ 实现堆排序 归并排序 快速排序
- 插入排序、冒泡排序、选择排序、希尔排序、快速排序、归并排序、堆排序和LST基数排序——C++实现
- 8 排序--选择排序,插入排序,冒泡排序,shell排序,快速排序(递归,迭代,改进版本),归并排序
- 堆排序,快速选择排序,快速选择排序,归并排序的c++实现
- 最简单之Java实现冒泡排序、选择排序、插入排序、希尔排序、归并排序和快速排序(转载请注明出处)
- 插入排序、冒泡排序、选择排序、希尔排序、高速排序、归并排序、堆排序和LST基数排序——C++实现
- c++ 实现 归并排序和快速排序