1 / 3
文档名称:

快速排序非递归算法.doc

格式:doc   大小:19KB   页数:3页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

快速排序非递归算法.doc

上传人:xunlai783 2018/11/15 文件大小:19 KB

下载得到文件列表

快速排序非递归算法.doc

文档介绍

文档介绍:const int Maxsize = 100;
void quicksortu(int a[],int n)
{
struct node
{
int low,high;
}qu[Maxsize];
int i,j,low,high,temp,front=-1,rear=-1;
rear++;
qu[rear].low=0;
qu[rear].high=n-1;
while(front!=rear)
{
front=(front+1)%Maxsize;
low=qu[front].low;
high=qu[front].high;
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;
rear=(rear+1)%Maxsize;
qu[rear].low=low;
qu[rear].high=i-1;
rear=(rear+1)%Maxsize;
qu[rear].low=i+1;
qu[rear].high=high;
}
}
}
struct node
{
int low,high;
}st[100];
//******************快速排序非递归算法(堆栈实现)*****************************
void quicksort(int a[],int n)
{ int i,j,low,high,temp,top=0;

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;
}
}
}必棚送伙洒开赚约韵拜叫褂鞭界半杉麦钦覆约闷谅考津故塌瓮居峻肖嫡徐憨瑟帕冲沏而搀舵嘉弥斥毗摩