写的不是太全
分治法(线性时间选择)
*线性时间选择问题*:给定线性序集中n个元素和一个整数k,1≤k≤n,要求找出这n个元素中第k小的元素,(这里给定的线性集是无序的)。
随机划分线性选择
线性时间选择随机划分法可以模仿随机化快速排序算法设计。基本思想是***对输入数组进行递归划分,与快速排序不同的是,它只对划分出的子数组之一进行递归处理***。
c++程序
//2d9-1 随机划分线性时间选择
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
int a[] = {5,7,3,4,8,6,9,1,2};
template <class Type>
void Swap(Type &x,Type &y);
inline int Random(int x, int y);
template <class Type>
int Partition(Type a[],int p,int r);
template<class Type>
int RandomizedPartition(Type a[],int p,int r);
template <class Type>
Type RandomizedSelect(Type a[],int p,int r,int k);
int main()
{
for(int i=0; i<9; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<RandomizedSelect(a,0,8,3)<<endl;
}
template <class Type>
void Swap(Type &x,Type &y)
{
Type temp = x;
x = y;
y = temp;
}
inline int Random(int x, int y)
{
srand((unsigned)time(0));
int ran_num = rand() % (y - x) + x;
return ran_num;
}
template <class Type>
int Partition(Type a[],int p,int r)
{
int i = p,j = r + 1;
Type x = a[p];
while(true)
{
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(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
{
//由于已知道子数组a[p:i]中的元素均小于要找的第k小元素
//因此,要找的a[p:r]中第k小元素是a[i+1:r]中第k-j小元素。
return RandomizedSelect(a,i+1,r,k-j);
}
}
程序解释:利用随机函数产生划分基准,将数组a[p:r]划分成两个子数组a[p:i]和a[i+1:r],使a[p:i]中的每个元素都不大于a[i+1:r]中的每个元素。接着"j=i-p+1"计算a[p:i]中元素个数j.如果k<=j,则a[p:r]中第k小元素在子数组a[p:i]中,如果k>j,则第k小元素在子数组a[i+1:r]中。注意:由于已知道子数组a[p:i]中的元素均小于要找的第k小元素,因此,要找的a[p:r]中第k小元素是a[i+1:r]中第k-j小元素。
在最坏的情况下,例如:总是找到最小元素时,总是在最大元素处划分,这是时间复杂度为O(n^2)。但平均时间复杂度与n呈线性关系,为O(n)(数学证明过程略过,可参考王云鹏论文《线性时间选择算法时间复杂度深入研究》
利用中位数线性时间选择
中位数:是指将数据按大小顺序排列起来,形成一个数列,居于数列中间位置的那个数据。
算法思路:如果能在线性时间内找到一个划分基准使得按这个基准所划分出的2个子数组的长度都至少为原数组长度的ε倍(0<ε<1),那么就可以在最坏情况下用O(n)时间完成选择任务。例如,当ε=9/10,算法递归调用所产生的子数组的长度至少缩短1/10。所以,在最坏情况下,算法所需的计算时间T(n)满足递推式T(n)<=T(9n/10)+O(n)。由此可得T(n)=O(n)。
c++代码
//2d9-2 中位数线性时间选择
#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;
}
int counts=0;
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]);
}
//找中位数的中位数
cout<<++counts<<": "<<p+(r-p-4)/5<<endl;
Type x = Select(a,p,p+(r-p-4)/5,(r-p-4)/10);
cout<<endl;
cout<<counts<<": "<<x<<endl;
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);
}
}
java代码
@Test
void test() {
//初始化数组
int[] a=new int[110];
for(int i=0; i<100; i++)
{
a[i] = (int)(Math.random()*500);
System.out.print("a["+i+"]="+a[i]+" ");
}
System.out.println();
System.out.println("第83个元素是"+Select(a,0,99,83));
//重新排序,对比结果
BubbleSort(a,0,99);
for(int i=0; i<100; i++)
{
System.out.print("a["+i+"]="+a[i]+" ");
}
}
//冒泡排序
int[] BubbleSort(int[] a,int p,int r)
{
//记录一次遍历中是否有元素的交换
boolean exchange;
for(int i=p; i<=r-1;i++)
{
exchange = false ;
for(int j=p; j<=r-1; j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
exchange = true;
}
}
//如果这次遍历没有元素的交换,那么排序结束
if(false == exchange)
{
break ;
}
}
return a;
}
int Partition(int[] a,int p,int r,int x)
{
int i = p-1,j = r + 1;
while(true)
{
while(a[++i]<x && i<r);
while(a[--j]>x);
if(i>=j)
{
break;
}
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
return i;
}
int Select(int[] a,int p,int r,int k)
{
if(r-p<75)
{
BubbleSort(a,p,r);
System.out.println(p+k-1+" "+a[p+k-1]);
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]交换位置
//使所有中位数都排列在数组最左侧,以便进一步查找中位数的中位数
a=BubbleSort(a,p+5*i,p+5*i+4);
int temp=a[p+5*i+2];
a[p+5*i+2]=a[p+i];
a[p+i]=temp;
}
//找中位数的中位数
int 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);
}
}
执行结果
Debug
可以看出最终的中位数全部位于数组元素的前面
查找中位数的中位数,递归进入
使用快速排序是的中位数。左边的数小于中位数,右边的大于中位数,并获取中位数索引,将该索引值+1与我们所需要的元素的序号进行比较。(因为序号第83个其实就是索引82,所有82要加1)
参考(https://blog.csdn.net/liufeng_king/article/details/8480430#)