/********************************************
* Author : Dong Huaan
* Email : [email protected]
* Filename : 11.0_SortAndSearch.cpp
* Creation time : 2018-01-06 23:24:49
* Last modified : 2018-01-07 17:18:44
* Description : ---
*******************************************/
#include
//***********************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();
}