快速排序非递归实现

#include<iostream>
using namespace std;
#define Maxsize 100
struct node
{
    int low;
 int high;
};
void quicksort(int  *a,int n)
{
 int i,j,low,high,tmp,top=-1;
 node stacks[Maxsize];
 top++;
 stacks[top].low=0;
 stacks[top].high=n-1;
 while(top>-1)
 {
  low=stacks[top].low;
  high=stacks[top].high;
  top--;
  i=low;
  j=high;
  if(low<high)
  {
   tmp=a[low];
   while(i!=j)
   {
    while(i<j&&a[j]>tmp) j--;
    if(i<j)
    {
     a[i]=a[j];
     i++;
    }
    while(i<j &&a[i]<tmp) i++;
    if(i<j)
    {
     a[j]=a[i];
     j--;
    }
   }
   a[i]=tmp;
   
   top++;
   stacks[top].low=low;
   stacks[top].high=i-1;
   top++;
   stacks[top].low=i+1;
   stacks[top].high=high;
  }
 }
 
}
int main()
{
    int a[10]={1,3,5,6,7,2,9,8,4};
 quicksort(a,9);
    for(int i=0;i<9;i++)
  cout<<a[i]<<" ";
 return 0;
}

你可能感兴趣的:(struct)