快速排序非递归算法

<textarea cols="86" rows="15" name="code" class="cpp">#include&quot;stdio.h&quot; #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&gt;-1)//当栈不空循环 { low=st[top].low;high=st[top].high;//出栈 top--; i=low;j=high; if(low&lt;high)//一次划分 { temp=a[low]; while(i!=j) { while(i&lt;j&amp;&amp;a[j]&gt;temp)j--; if(i&lt;j){a[i]=a[j];i++;}/*这个做法很妙,免去了《数据结构》书中的交换的代码*/ while(i&lt;j&amp;&amp;a[i]&lt;temp)i++; if(i&lt;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&lt;10;i++)printf(&quot;%d &quot;,a[i]);printf(&quot;/n&quot;); } </textarea>

你可能感兴趣的:(快速排序非递归算法)