算法设计与分析 快速排序的递归实现算法
2017-07-07 09:51
691 查看
输入:先输入进行合并排序元素的个数,然后依次随机输入(或随机生成)每个数字。
输出:元素排序后的结果。
示例:输入:8 9 1 2 4 8 6 15 8,输出:1 2 4 6 8 8 9 15
代码中包含两种,一种是普通快排,一种是生成随机数的快排
//快速排序
#include<iostream>
#include <algorithm>
using namespace std;
template<class Type>
int Partition(Type a[],int p,int r)
{
int i=p,j=r+1;
Type x=a[p];
while(i<j)
{
while(a[++i]<x&&i<r);
while(a[--j]>x);
if(i>=j)break;
swap(a[i],a[j]);
}
a[p]=a[j];
a[j]=x;
return j;
}
template<class Type>
void QuickSort(Type a[],int p,int r)
{
if(p<r)
{
int q=Partition(a,p,r);
QuickSort(a,p,q-1);
QuickSort(a,q+1,r);
}
}
#include <algorithm>
template<class Type>
int Partition(Type a[],int p,int r)
{
int i=p,j=r+1;
Type x=a[p];
while(i<j)
{
while(a[++i]<x&&i<r);
while(a[--j]>x);
if(i>=j)break;
swap(a[i],a[j]);
}
a[p]=a[j];
a[j]=x;
return j;
}
template<class Type>
int RandomizedPartition(Type a[],int p,int r)
{
int i=random()%(r-p)+p;
swap(a[i],a[p]);
return Partition(a,p,r);
}
template<class Type>
void RandomizedQuickSort(Type a[],int p,int r)
{
if(p<r){
int q=RandomizedPartition(a,p,r);
RandomizedQuickSort(a,p,q-1);
RandomizedQuickSort(a,q+1,r);
}
}
int main()
{
int n, data[100];
cout << "请输入数组的长度" << endl;
cin >> n;
cout << "请输入数组" << endl;
for (int i = 0; i < n; i++)
{
cin >> data[i];
}
RandomizedQuickSort(data,0,n-1);
for (int i = 0; i < n; i++)
{
cout << data[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
输出:元素排序后的结果。
示例:输入:8 9 1 2 4 8 6 15 8,输出:1 2 4 6 8 8 9 15
代码中包含两种,一种是普通快排,一种是生成随机数的快排
//快速排序
#include<iostream>
#include <algorithm>
using namespace std;
template<class Type>
int Partition(Type a[],int p,int r)
{
int i=p,j=r+1;
Type x=a[p];
while(i<j)
{
while(a[++i]<x&&i<r);
while(a[--j]>x);
if(i>=j)break;
swap(a[i],a[j]);
}
a[p]=a[j];
a[j]=x;
return j;
}
template<class Type>
void QuickSort(Type a[],int p,int r)
{
if(p<r)
{
int q=Partition(a,p,r);
QuickSort(a,p,q-1);
QuickSort(a,q+1,r);
}
}
#include <algorithm>
template<class Type>
int Partition(Type a[],int p,int r)
{
int i=p,j=r+1;
Type x=a[p];
while(i<j)
{
while(a[++i]<x&&i<r);
while(a[--j]>x);
if(i>=j)break;
swap(a[i],a[j]);
}
a[p]=a[j];
a[j]=x;
return j;
}
template<class Type>
int RandomizedPartition(Type a[],int p,int r)
{
int i=random()%(r-p)+p;
swap(a[i],a[p]);
return Partition(a,p,r);
}
template<class Type>
void RandomizedQuickSort(Type a[],int p,int r)
{
if(p<r){
int q=RandomizedPartition(a,p,r);
RandomizedQuickSort(a,p,q-1);
RandomizedQuickSort(a,q+1,r);
}
}
int main()
{
int n, data[100];
cout << "请输入数组的长度" << endl;
cin >> n;
cout << "请输入数组" << endl;
for (int i = 0; i < n; i++)
{
cin >> data[i];
}
RandomizedQuickSort(data,0,n-1);
for (int i = 0; i < n; i++)
{
cout << data[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
相关文章推荐
- 用递归法:设计算法求解汉诺塔问题,并编程实现。 (1) Hanoi(汉诺)塔问题分析 这是一个古典的数学问题,是一个用递归方法解题的典型例子。问题是这样的:古代有一个梵塔,塔内有3个座 A,B,C
- [Java算法分析与设计]二叉树非递归实现遍历
- 算法设计、分析与实现 从入门到精通 C、C++和Java 这本书的堆实现85页C++语言实现有问题
- 二叉树采用二叉链表结构表示。设计并实现如下算法:后序递归建树,先序非递归遍历该树。
- 第K大数 算法分析、设计与实现(Java)
- 【算法设计与分析】递归与分治----2.4 排列问题
- 递归下降语法分析程序的设计与实现
- 算法设计与分析--霍夫曼树编码(C++实现)
- 算法设计与分析--N皇后问题实现程…
- 算法设计之快速排序的随机化版本 (C++实现)
- 三杯水问题 算法分析、设计与实现(Java)
- 使用递归下降算法分析数学表达式 -- 基于堆栈的计算器实现算法
- 编译原理实验之预测分析算法的设计与实现
- 常用算法的递归实现问题分析(针对《数据结构与程序设计》by Robert.L.Kruse)
- 【算法设计与分析】8、哈弗曼编码,贪心算法实现
- 归并排序和快速排序比较【算法设计与分析实验报告】
- 算法设计与分析之循环与递归
- 一步步学习数据结构和算法之快速排序效率分析及java实现
- 区域增长算法的设计与实现(递归思想和堆栈数据结构)
- [算法] 快速排序的非递归形式实现