1.递归实现
#include <stdio.h>
#include<stdlib.h>
void quicksort(int a[],int s,int t){
int i=s,j=t;
if(s<t){
int key =a[s];
do{
while (j>i&&a[j]>=key)
j--;
if(i<j)
{ a[i]=a[j];
i++;
}
while(i<j&&a[i]<=key)
i++;
if(i<j)
{
a[j]=a[i];
j--;
}
}while(i<j);
a[i]=key;
quicksort(a,s,j-1);
quicksort(a,j+1,t);
}
}
main(){
int a[]={4,3,8,5,2,1};
int i=0;
for(i=0;i<6;i++){
printf("%d",a[i]);
}
printf("\n");
quicksort(a,0,5);
for(i=0;i<6;i++){
printf("%d",a[i]);
}
}
struct node
{
int min;
int max;
};
void fun(int min,int max,int a[])
{
int key = a[min];
int i = min;
int j = max;
int temp;
struct node Stack[100];
int top = 0;
Stack[top].min = min;
Stack[top].max = max;
while(top>-1)
{
//min max记录当前处理的这个区间的左极限和有极限
i = min = Stack[top].min;
j = max = Stack[top].max;
top--;
key = a[min];
while(i<j)
{
while((i<j) && (key <= a[j]))
{j--;}
if(i < j)
{
temp = a[i];a[i] = a[j];a[j] = temp;
i++;
}
while((i<j) && (key >= a[i]))
{i++;}
if(i < j)
{
temp = a[i];a[i] = a[j];a[j] = temp;
j--;
}
}//处理一次 即将比绑定值小的全部放左边 比绑定值大的放右边
if(min < i-1)
{
top++;
Stack[top].min = min;
Stack[top].max = i-1;
}
if(max>i+1)
{
top++;
Stack[top].min = i+1;
Stack[top].max = max;
}
}
}
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;
}