快排递归非递归

#include <iostream>
using namespace std;

int partition(int *a, int l, int h)
{
	int x = a[l];
	int i = l;
	int j = h+1;
	int temp;
	
	while (i<j)
	{
		while (a[++i]<x&&i<h);
		while(a[--j]>x);
		if (i<j)
		{
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
		}
	}

	a[l] = a[j];
	a[j] = x;
	return j;
}

void qsort(int *a, int l, int h)
{
	if(l>=h)
		return;
	
	int q = partition(a, l, h);
	
	qsort(a, l, q-1);

	qsort(a, q+1, h);
}

int main()
{
	int a[9] = {9,8,7,6,5,4,3,2,1};
	qsort(a,0,8);
	
	for(int i=0; i<9; i++)
		cout<<a[i]<<endl;
	
	return 0;
}


//非递归算法
//数据规模很大时,递归的算法很容易导致栈溢出,改为非递归,模拟栈操作,最大长度为n,每次压栈时先压长度较大的,此时栈深度为logn。
#include <iostream>
using namespace std;

int partition(int *a, int l, int h)
{
	int x = a[l];
	int i = l;
	int j = h+1;
	int temp;
	
	while (i<j)
	{
		while (a[++i]<x&&i<h);
		while(a[--j]>x);
		
		if (i<j)
		{
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
		}
	}
	
	a[l] = a[j];
	a[j] = x;

	return j;
}

void qsort(int *a, int l, int h)
{
	if (l>=h)
		return;

	int *s = new int[h-l+1];
	int p = 0;
	s[p++] = l;
	s[p++] = h;

	int low,high,q;

	while (p>0)
	{
		high = s[--p];
		low = s[--p];
		if (low>=high)
			break;
	
		q = partition(a, low, high);
 
		if (q-low > high-q)
		{
			s[p++] = low;
			s[p++] = q-1;

			if (high > q)
			{
				s[p++] = q+1;
				s[p++] = high;
			}

		}
		else
		{
			s[p++] = q+1;
			s[p++] = high;
			if (q > low)
			{
				s[p++] = low;
				s[p++] = q-1;
			}
		}
	}
	delete []s;
}

int main()
{
	int a[9] = {9,8,7,6,5,4,3,2,1};
	//int a[9] = {1,2,3,4,5,6,7,8,9};
	qsort(a,0,8);
	 
	for (int i=0; i<9; i++)
		cout<<a[i]<<endl;

	return 0;
}







//******************快速排序非递归算法(队列实现)*****************************
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;
           }
       }
}













  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值