#include
int Partition(int a[],int low,int high)
{
int pivotkey = a[low];
while(low
{
if(low=pivotkey) --high;
a[low]=a[high];
if(low
a[high]=a[low];
}
a[low]=pivotkey;
return low;
}
void Quick_Sort(int a[],int low,int high)
{
if(low
{
int position = Partition(a,low,high);
Quick_Sort(a,low,poisition-1);
Quick_Sort(a,poisition+1,high);
}
}
void main()
{
int a[4]={45,56,23,5};
Quick_Sort(a,0,3);
}
改进版:
void fun(int min,int max,int a[])
{
int key = a[min];
int i = min;
int j = max;
int temp;
if(min>=max)
return;
while(i
{
while((i
{j--;}
if(key > a[j])
{ a[i] = a[j]; a[j] = key; i++; }
while((i= a[i]))
{i++;}
if(key < a[i])
{ a[j] = a[i];a[i] = key; j--; }
}
fun(min,i-1,a);
fun(i+1,max,a);
}
int main()
{
int i;
int a[10] = {49,38,65,97,76,13,27,9,2,1};
for(i=0;i<10;i++)
printf(" %d ",a[i]);
printf("\n");
fun(0,9,a);
for(i=0;i<10;i++)
printf(" %d ",a[i]);
printf("\n");
return 0;
}