实现快速排序的非递归

实现快速排序的非递归

关于排序的其他算法如冒泡、插入、希尔等排序方法见:http://blog.csdn.net/step_ma/article/details/74115184
这里主要用非递归实现快速排序,化为子区间时可采用左右指针法、挖坑法和前后指针法,这里测的是挖坑法,其他两种均可实现,关于各种 主要是利用栈实现。
代码实现:
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
        
        
using namespace std;

int PartSort2(int *a, int start, int end)          //挖坑法
{
	//int a[] = { 3, 1, 4, 9, 5, 8, 3, 0, 2 };
	int key = a[end];
	while (start < end)
	{
		while (start < end && a[start] <= key)
		{
			++start;
		}
		if (start < end)
		{
			a[end] = a[start];
		}

		while (start < end && a[end] >= key)
		{
			--end;
		}

		if (start < end)
		{
			a[start] = a[end];
		}
	}
	a[start] = key;
	return start;
}

void QuickSort(int* a, int left, int right)             //非递归实现,利用栈
{
	stack
        
        
         
          t;
	if (left
         
         
           left) { t.push(left); t.push(div - 1); } if (div + 1 
          
            l) { t.push(l); t.push(div - 1); } if (div + 1 
            
           
         
        
        
       
       
      
      
     
     
测试结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值