快速排序算法
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
#include "math.h"
int partion(int a[],int low ,int high)
{
int pivotkey=a[low];
while(low<high)
{
while(low<high && a[high]>=pivotkey)
{
high--;
}
a[low]=a[high]; //比pivotkey小的移到低端,且high位置空闲
while(low<high && a[low]<=pivotkey)
{
low++; //被a[high]值覆盖的a[low],由于a[low]<=pivotkey肯定成立,low++
}
a[high]=a[low]; //比pivotkey大的移到高端,且low位置空闲
}
a[low]=pivotkey;
return low;
}
void Qsort(int a[],int low ,int high)
{
int pivotloc;
if(low<high)
{
pivotloc=partion(a,low,high);
Qsort(a,low,pivotloc-1);
Qsort(a,pivotloc+1,high);
}
}
void quick_sort(int a[],int n)
{
Qsort(a,0,n-1);
}
void main()
{
int a[10]={14,16,13,18,7,20,5,45,6,30};
int i;
quick_sort(a,10);
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
}