快速排序非递归实现
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;
}
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;
}
相关文章推荐
- 快速排序partition过程常见的两种写法+快速排序非递归实现
- 快速排序partition过程常见的两种写法+快速排序非递归实现
- 霍尔快速排序 非递归 C++实现
- 快速排序的三种实现方式以及非递归版本
- 快速排序的非递归实现
- 快速排序、冒泡排序、堆排序、shell排序的递归和非递归实现
- 快速排序c语言递归和栈实现
- 快速排序实现之递归与非递归
- C++ 快速排序 递归实现
- java 递归实现快速排序
- 快速排序的递归和非递归实现方法
- C/C++面试题(三) 推断二叉树、快速排序递归实现、递归判断数组递增
- 快速排序非递归c#实现
- Java 递归与非递归实现快速排序
- 快速排序C++实现(递归,非递归)
- 快速排序的非递归实现
- 快速排序的非递归实现代码
- 快速排序非递归实现
- 快速排序 的原理及其java实现(递归与非递归)
- 快速排序--递归实现