快速排序的非递归实现 -- 人人网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;
}

到此程序结束!


如果有疑问请留言交流!

目录
相关文章
|
8天前
|
机器学习/深度学习 搜索推荐 算法
【洛谷 P1177】【模板】快速排序 题解(快速排序+指针)
**快速排序模板题解** - **任务**:对输入的N个整数进行排序。 - **算法**:使用快速排序,避免使用C++的STL`sort`。 - **输入**:一行包含N(N≤10^5),第二行是N个不超过10^9的整数。 - **输出**:排序后的整数序列,空格分隔。 - **样例**:输入`5 4 2 4 5 1`,输出`1 2 4 4 5`。 - **提示**:20%的数据,N≤10^3;所有数据,N≤10^5。 - **代码**:定义`partition`函数划分数组,主函数`main`读取数据,调用`quickSort`排序,然后打印结果。
7 0
|
23天前
|
Java
P9242 [蓝桥杯 2023 省 B] 接龙数列JAVA,边权为1的最短路问题,洛谷P9242 [蓝桥杯 2023 省 B] 接龙数列​编辑力扣1926.迷宫离入口最近的出口力扣433.
P9242 [蓝桥杯 2023 省 B] 接龙数列JAVA,边权为1的最短路问题,洛谷P9242 [蓝桥杯 2023 省 B] 接龙数列​编辑力扣1926.迷宫离入口最近的出口力扣433.
|
7月前
|
算法
代码随想录算法训练营第五十四天 | LeetCode 392. 判断子序列、115. 不同的子序列
代码随想录算法训练营第五十四天 | LeetCode 392. 判断子序列、115. 不同的子序列
49 1
|
7月前
|
算法
代码随想录算法训练营第二十八天 | LeetCode 491. 递增子序列、46. 全排列、47. 全排列 II
代码随想录算法训练营第二十八天 | LeetCode 491. 递增子序列、46. 全排列、47. 全排列 II
42 0
|
11月前
力扣面试题 08.06. 汉诺塔问题:思路分析+图文详解+代码实现
力扣面试题 08.06. 汉诺塔问题:思路分析+图文详解+代码实现
119 0
冒泡排序(简练版)(一看就懂)(对比选择排序)
冒泡排序(简练版)(一看就懂)(对比选择排序)
|
算法 搜索推荐 JavaScript
【HDU 1425】 一个案例明白冒泡排序和快速排序
【HDU 1425】 一个案例明白冒泡排序和快速排序
89 0
|
机器学习/深度学习 算法
算法刷题第五天:双指针--4
链表的缺点在于不能通过下标访问对应的元素。因此我们可以考虑对链表进行遍历,同时将遍历到的元素依次放入数组A中。如果我们遍历到了N个素,那么链表以及数组的长度也为N,对应的中间节点即为A[N/2] 。
79 1
算法刷题第五天:双指针--4
|
机器学习/深度学习 算法 JavaScript
算法刷题第四天:双指针--3
算法刷题第四天:双指针--3
64 0
算法刷题第四天:双指针--3
|
算法 Java
代码随想录算法训练营第十天 | KMP算法 字符串总结 双指针回顾
代码随想录算法训练营第十天 | KMP算法 字符串总结 双指针回顾
87 0