快速排序的非递归实现 -- 人人网2014笔试题目

简介:

快速排序是各大公司笔试面试中出现频率最高的几个之一,因为它在实际应用中经常用到!

在看快速排序的的非递归之前我们先看看几种经典的快速排序的递归的实现。

/*
*BLOG:http://blog.csdn.net/wdzxl198
*AUTHOR:Atlas
*EMAIL:wdzxl198@163.com
*/

#include <iostream>
using namespace std;

//算法导论上的实现 ,quicksort_1 
int Partition(int array[],int lhs,int rhs)
{
	int x = array[rhs];
	int i = lhs - 1;
	
	for(int j=lhs;j<=rhs-1;j++)
	{
		if(array[j] <= x)
		{
			++i;
			std::swap(array[i],array[j]);
		}
	}
	std::swap(array[i+1],array[rhs]);
	return i+1;
}
void quickSort_1(int array[],int lhs,int rhs)
{
	while(lhs < rhs)
	{
		int q = Partition(array,lhs,rhs); 
		quickSort_1(array,lhs,q-1);
		lhs = q + 1;	
//		quickSort(array,q+1,rhs);	//消除尾递归,利用迭代控制 
	}
}
//编程珠玑算法 
//second,quciksort_2----Simplest version, Lomuto partitioning 
void quickSort_2(int a[],int lhs,int rhs)
{
	if(lhs >= rhs)
		return;
	int m = lhs;
	for(int i=lhs+1;i<=rhs;i++)
	{
		if(a[i] < a[lhs])
			swap(a[++m],a[i]);
	} 
	swap(a[lhs],a[m]);
	quickSort_2(a,lhs,m-1);
	quickSort_2(a,m+1,rhs);
} 
//third,使用双向划分Two-way partitioning
void quickSort_3(int a[],int lhs,int rhs)
{
	if(lhs >= rhs)
		return;
	int t = a[lhs],i = 1,j = rhs+1;
	for(;;)
	{
		do
		{
			i++;
		}while(i<=rhs && a[i] < t);
		do
		{
			j--;
		}while(a[j] > t);
		if(i > j)
			break;
		swap(a[i],a[j]);
	} 
	swap(a[lhs],a[j]);
	quickSort_3(a,lhs,j-1);
	quickSort_3(a,j+1,rhs);
}
//forth,整体使用快排划分,选取元素采用随机方法,然后基本有序后使用插入排序来进行
//qsort3 + randomization + isort small subarrays + swap inline 
int randint(int l, int u)
{	
	return l + (RAND_MAX*rand() + rand()) % (u-l+1);
}
void isort3(int a[],int lhs,int rhs)
{
	int i ,j;
	for(i=lhs;i<=rhs;i++)
	{
		int temp = a[i];
		for(
		j =i;j>0 && a[j-1]>temp;j--)
		{
			a[j] = a[j-1];
		}
		a[j] = temp;
	}
}

const int cutoff = 3;
void quickSort_4(int a[],int lhs,int rhs)
{
	if(rhs - lhs < cutoff)
		return;
	int randomdata = randint(lhs,rhs);	
	swap(a[lhs],a[randomdata]);
	int t =a[lhs],i = lhs,j = rhs + 1;
	for(;;)
	{
		do
		{
			i++;
		}while(i<=rhs && a[i] < t);
		do
		{
			j--;
		}while(a[j] > t);
		if(i > j)
			break;
		int temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
	swap(a[lhs],a[j]);
	quickSort_4(a,lhs,j-1);
	quickSort_4(a,j+1,rhs);
}

测试程序请读者自行设计。

总结这么几种递归的方式,可以看到是同栈结构就可以实现快速排序的非递归实现。以下就是使用STL的方法实现.

/*
*BLOG:http://blog.csdn.net/wdzxl198
*AUTHOR:Atlas
*EMAIL:wdzxl198@163.com
*/

#include<iostream>
#include<stack>

using namespace std;

int Partition(int array[],int lhs,int rhs)
{
	int x = array[rhs];
	int i = lhs - 1;
	
	for(int j=lhs;j<=rhs-1;j++)
	{
		if(array[j] <= x)
		{
			++i;
			std::swap(array[i],array[j]);
		}
	}
	std::swap(array[i+1],array[rhs]);
	return i+1;
}

void QuickSort(int *arr,int left,int right)
{
    stack<int> st;
    if(left < right)
    {
        int mid = Partition(arr,left,right);
        if(left < mid-1)
	{
            st.push(left);
            st.push(mid-1);
        }
        if(mid+1 < right)
	{
            st.push(mid+1);
            st.push(right);
        }
        
        while(!st.empty())
	{
            int q = st.top();
            st.pop();
            int p = st.top();
            st.pop();
            
            mid = Partition(arr,p,q);
            if(p < mid-1)
	    {
                st.push(p);
                st.push(mid-1);
            }
            if(mid+1 < q)
	    {
                st.push(mid+1);
                st.push(q);
            }       
        }
    }
    else
    	return;
}

int main()
{
	int a[10] ={3,7,6,4,0,2,9,8,1,5};  
	
	for(int i=0;i<10;i++)
	   cout<<a[i]<<" ";
	cout<<endl; 
	cout<<"快速排序:";	
	QuickSort(a,0,9);

	
	for(int i=0;i<10;i++)
		cout<<a[i]<<" ";
	
	cout<<endl;
	system("PAUSE");
	return 0;
}

到此程序结束!


如果有疑问请留言交流!

目录
相关文章
|
存储 SQL 关系型数据库
OceanBase与MySQL有何区别?
【8月更文挑战第12天】OceanBase与MySQL有何区别?
4200 3
|
Web App开发 XML JavaScript
|
分布式计算 资源调度 Spark
Spark RDD类源码阅读
每天进步一点点~开搞~ abstract class RDD[T: ClassTag]( //@transient 注解表示将字段标记为瞬态的 @transient private var _sc: SparkContext, // Seq是序列,元素有插入的先后顺序,可以有重复的元素。
1164 0
HTTP 错误 500.19 - Internal Server Error
错误信息如下: 今天下午尝试,先在iis中新建网站,再在该网站目录下新建应用程序,具体步骤如下: 1、新建网站JianKunKingServices 2、在网站JianKunKingServices目录下新建应用程序: 3、点击确定,完成如下图: 4、预览WcfService.svc文件,报出上图的错误。 错误原因是: 在第二步的时候,忽略了应用程序池的选择,修改应用程序池即可:
1462 0
|
C# 索引
C# 的快捷键汇总(二)
文本操作快捷键 ——〉 在文本编辑器中使用下列快捷键组合在打开文档中删除、移动或者格式化文本   命令名             快捷键                   说明 编辑.分行 Enter 插入一个新行。
1297 0
|
15天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23514 12
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
4天前
|
人工智能 BI 持续交付
Claude Code 深度适配 DeepSeek V4-Pro 实测:全场景通关与真实体验报告
在 AI 编程工具日趋主流的今天,Claude Code 凭借强大的任务执行、工具调用与工程化能力,成为开发者与自动化运维的核心效率工具。但随着原生模型账号稳定性问题频发,寻找一套兼容、稳定、能力在线的替代方案变得尤为重要。DeepSeek V4-Pro 作为新一代高性能大模型,提供了完整兼容 Claude 协议的 API 接口,只需简单配置即可无缝驱动 Claude Code,且在任务执行、工具调用、复杂流程处理上表现极为稳定。
1277 3
|
9天前
|
人工智能 缓存 Shell
Claude Code 全攻略:命令大全 + 实战工作流(完整版)
Claude Code 是一款运行在终端环境下的 AI 编码助手,能够直接在项目目录中理解代码结构、编辑文件、执行命令、执行开发计划,并支持持久化记忆、上下文压缩、后台任务、多模型切换等专业能力。对于日常开发、项目维护、快速重构、代码审查等场景,它可以大幅减少手动操作、提升编码效率。本文从常用命令、界面模式、核心指令、记忆机制、图片处理、进阶工作流等维度完整说明,帮助开发者快速上手并稳定使用。
2342 4