#include<stdio.h>
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int partition(int a[], int low, int high)
{
int privotKey = a[low]; //privotKey为基准元素
while(low < high)
{
while(low < high && a[high] >= privotKey)
{
--high;
}
swap(&a[low], &a[high]);
while(low < high && a[low] <= privotKey )
{
++low;
}
swap(&a[low], &a[high]);
}
return low;
}
void quickSort(int a[], int low, int high)
{
if(low < high)
{
int privotLoc = partition(a, low, high); //将表分为两部分
quickSort(a, low, privotLoc -1);
quickSort(a, privotLoc + 1, high);
}
}
int main(){
int a[10] = {3,1,5,7,2,4,9,6,10,8};
quickSort(a,0,9);
for(int i=0;i<10;i++)
{
printf("%d ",a[i]);
}
while(1);
}
思路:先取个基准元素将表分为两部分,一部分为小于基准元素,另一部分大于基准元素,再将这两部分重复该过程直道排序完成。
优缺点:性能平稳,但是不稳定。