您的位置:首页 > 其它

快速排序非递归实现

2012-07-08 00:01 323 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: