标签:
快速排序将以枢轴为界,将原数组分为两个部分,枢轴以前,值都小于枢轴的值,枢轴以后的值都大于枢轴。相较于归并排序和堆排序,快速排序是一种高效而且简单的排序方法。
int partion(SqList* list, int low, int high) {//返回枢轴下标 int key = list->data[high];//枢轴值 int fast = low; int slow = low; while (fast < high) { if (list->data[fast] < key) { if (slow != fast) { swap(list->data[fast], list->data[slow]); } fast++; slow++; } else { fast++; } } swap(list->data[slow], list->data[high]); return slow; } void qsort1(SqList* list,int low,int high) { if (low < high) { int index = partion(list,low,high); qsort1(list, low, index - 1);//高低子表都采用递归的方法实现 qsort1(list, index + 1, high); } } #define MAX_LENGTH_INSERT_SORT 7//定义插入排序可以接受的最大数组 void qsort2(SqList* list, int low, int high) { if ((high - low) > MAX_LENGTH_INSERT_SORT) { if (low < high) { int index = partion(list, low, high); qsort2(list, low, index - 1); qsort2(list, index + 1, high); } } else InsertSort2(list); } //只用一次递归,高子表采用迭代的方式,减小了递归的深度。 void qsort3(SqList* list, int low, int high) { if ((high - low) > MAX_LENGTH_INSERT_SORT) { if (low < high) { int index = partion(list, low, high); qsort3(list, low, index - 1); low = index + 1;//高子表不递归,采用迭代 } } else InsertSort2(list);//当然,此处不能直接使用,还需要对InsertSort2作一下修改,改成指定下标的形式 } void QuickSort(SqList* list) { qsort1(list,0,list->length-1); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/walker19900515/article/details/46691003