2.9 线性时间选择
20201003
参考链接:https://blog.youkuaiyun.com/liufeng_king/article/details/8480430
https://www.cnblogs.com/cc1997/p/7860296.html
https://my.oschina.net/bgbfbsdchenzheng/blog/499857(精品)
这一节,我们先从放上代码,然后后期再来看这个代码的实现以及相关的例子。
1.方法一:基于快速排序的算法改进
1.1.算法分析
关于排序算法,我们就不多讲解了,直接放上一张图,感兴趣的可以查看我的另外一篇文章:https://www.yuque.com/jiaozi-qjr0o/ysl4nc/yuq25g
如上排序算法,主要是选取第一个(相对于划分区域)作为基准值,基准值的选取决定了划分是否对称,从而决定了快速排序的运行时间是否快速。因此基于快速时间排序,我们对基准值进行随机选取,减少基准值选取对对称划分的影响。
1.2.代码实现
// partition.cpp : Defines the entry point for the console application.
//
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
#include "ctime"
#define N 1024
inline int Random(int x, int y)
{
srand((unsigned)time(0));
int ran_num = rand() % (y - x) + x;
return ran_num;
}
template <class Type>
void Swap(Type &x,Type &y)
{
Type temp = x;
x = y;
y = temp;
}
template<class Type>
int Partition(Type a[], int p, int r)
{
int i=p,j=r+1;//注意r+1
Type x=a[p];
//将<x的元素交换到左边区域
//将>x的元素交换到右边区域
while(true){
while(a[++i]<x && i<r);//左边元素小于基准值x且左标记i<r
while(a[--j]>x);//右边元素大于基准值x
if(i>=j)break;//当i>=j时,左右标记相遇
Swap(a[i],a[j]);//左边元素大于基准值x或者右边元素小于a[p]
}
a[p]=a[j];//i,j相遇处a[j]属于基准左边,因此将a[j]放在a[p]处
a[j]=x;//a[j]为基准值x
return j;//返回基准下标
}
template<class Type>
int RandomizedPartition(Type a[],int p,int r)//产生随机基准值的函数
{
int i=Random(p,r);
Swap(a[i],a[p]);
return Partition(a,p,r);
}
template<class Type>
Type RandomizedSelect(Type a[],int p,int r,int k)
{
if(p==r) return a[p];
int i=RandomizedPartition(a,p,r);
int j=i-p+1;
if(k<=j)return RandomizedSelect(a,p,i,k);
else return RandomizedSelect(a,i+1,r,k-j);
}
int main(int argc, char* argv[])
{
int n,count,i=0;
int a[N];
cout<<"请问你需要输入多少元素呢!"<<endl;
cin>>count;
for(i=0;i<count;i++){
cout<<"请输入第"<<i<<"个元素"<<endl;
cin>>n;
a[i]=n;
}
RandomizedSelect(a,0,count-1,3);//注意count-1
for(i=0;i<count;i++){
cout<<"第"<<i<<"个元素为"<<a[i]<<endl;
}
return 0;
}
2.方法二:基于中位数的线性时间选择
原文链接:https://blog.youkuaiyun.com/liufeng_king/article/details/8480430
1.1.算法分析
中位数:是指将数据按大小顺序排列起来,形成一个数列,居于数列中间位置的那个数据。
算法思路:如果能在线性时间内找到一个划分基准使得按这个基准所划分出的2个子数组的长度都至少为原数组长度的ε倍(0<ε<1),那么就可以在最坏情况下用O(n)时间完成选择任务。例如,当ε=9/10,算法递归调用所产生的子数组的长度至少缩短1/10。所以,在最坏情况下,算法所需的计算时间T(n)满足递推式T(n)<=T(9n/10)+O(n)。由此可得T(n)=O(n)。
算法步骤:
1.2.代码实现
//2d9-2 中位数线性时间选择
#include "stdafx.h"
#include <ctime>
#include <iostream>
using namespace std;
template <class Type>
void Swap(Type &x,Type &y);
inline int Random(int x, int y);
template <class Type>
void BubbleSort(Type a[],int p,int r);
template <class Type>
int Partition(Type a[],int p,int r,Type x);
template <class Type>
Type Select(Type a[],int p,int r,int k);
int main()
{
//初始化数组
int a[100];
//必须放在循环体外面
srand((unsigned)time(0));
for(int i=0; i<100; i++)
{
a[i] = Random(0,500);
cout<<"a["<<i<<"]:"<<a[i]<<" ";
}
cout<<endl;
cout<<"第83小元素是"<<Select(a,0,99,83)<<endl;
//重新排序,对比结果
BubbleSort(a,0,99);
for(int i=0; i<100; i++)
{
cout<<"a["<<i<<"]:"<<a[i]<<" ";
}
cout<<endl;
}
template <class Type>
void Swap(Type &x,Type &y)
{
Type temp = x;
x = y;
y = temp;
}
inline int Random(int x, int y)
{
int ran_num = rand() % (y - x) + x;
return ran_num;
}
//冒泡排序
template <class Type>
void BubbleSort(Type a[],int p,int r)
{
//记录一次遍历中是否有元素的交换
bool exchange;
for(int i=p; i<=r-1;i++)
{
exchange = false ;
for(int j=i+1; j<=r; j++)
{
if(a[j]<a[j-1])
{
Swap(a[j],a[j-1]);
exchange = true;
}
}
//如果这次遍历没有元素的交换,那么排序结束
if(false == exchange)
{
break ;
}
}
}
template <class Type>
int Partition(Type a[],int p,int r,Type x)
{
int i = p-1,j = r + 1;
while(true)
{
while(a[++i]<x && i<r);
while(a[--j]>x);
if(i>=j)
{
break;
}
Swap(a[i],a[j]);
}
return j;
}
template <class Type>
Type Select(Type a[],int p,int r,int k)
{
if(r-p<75)
{
BubbleSort(a,p,r);
return a[p+k-1];
}
//(r-p-4)/5相当于n-5
for(int i=0; i<=(r-p-4)/5; i++)
{
//将元素每5个分成一组,分别排序,并将该组中位数与a[p+i]交换位置
//使所有中位数都排列在数组最左侧,以便进一步查找中位数的中位数
BubbleSort(a,p+5*i,p+5*i+4);
Swap(a[p+5*i+2],a[p+i]);
}
//找中位数的中位数
Type x = Select(a,p,p+(r-p-4)/5,(r-p-4)/10);
int i = Partition(a,p,r,x);
int j = i-p+1;
if(k<=j)
{
return Select(a,p,i,k);
}
else
{
return Select(a,i+1,r,k-j);
}
}