版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010095372/article/details/84109785
快速排序就是对冒泡法进行一种改进,基本思想基于分治法,在L[1…n]选一个数为基准,比他小的往前,比他大的往后,这样得到两个L,在这个两个L中继续挑基准,小的往前,大的往后,循序往复,直到只有一个数字就OK了。
一般选第一个
代码如下
//选择排序
int Pratition(int a[], int low, int high){
int pivot = a[low];
while (low < high) {
while (low < high && a[high] > pivot) {
high--;
}
a[low] = a[high];
while (low < high && a[low] < pivot) {
low++;
}
a[high] = a[low];
}
a[low] = pivot;
return low;
}
void QuickSort(int a[], int low, int high)
{
if(low < high) {
int pivotopos = Pratition(a, low, high);
QuickSort(a, low, pivotopos-1);
QuickSort(a, pivotopos+1, high);
}
}
4210359768 0,5,9
3210459768 0,4,4
0213459768 0,3,3
0213459768 0,0,2
0123459768 1,2,2
0123458769 6,9,9
0123456789 6,8,8
0123456789 6,6,7