#include"stdio.h" #define Maxsize 100 void quicksort(int a[],int n) { int i,j,low,high,temp,top=-1; struct node { int low,high; }st[Maxsize]; //栈 top++; st[top].low=0;st[top].high=n-1;//首次入栈 while(top>-1)//当栈不空循环 { low=st[top].low;high=st[top].high;//出栈 top--; i=low;j=high; if(low<high)//一次划分 { temp=a[low]; while(i!=j) { while(i<j&&a[j]>temp)j--; if(i<j){a[i]=a[j];i++;}/*这个做法很妙,免去了《数据结构》书中的交换的代码*/ while(i<j&&a[i]<temp)i++; if(i<j){a[j]=a[i];j--;} } a[i]=temp; top++;st[top].low=low;st[top].high=i-1;/*新入栈*/ top++;st[top].low=i+1;st[top].high=high;/*新入栈*/ } } } int main() { int a[10]={189,987,654,321,548,254,658,845,125,489}; int i,n=10; quicksort(a,n); for(i=0;i<10;i++)printf("%d ",a[i]);printf("/n"); }