線性時間選擇


         線性時間選擇問題:給定線性序集中n個元素和一個整數k,1≤k≤n,要求找出這n個元素中第k小的元素,(這里給定的線性集是無序的)。

       1、隨機划分線性選擇

       線性時間選擇隨機划分法可以模仿隨機化快速排序算法設計。基本思想是對輸入數組進行遞歸划分,與快速排序不同的是,它只對划分出的子數組之一進行遞歸處理。

程序如下:

//2-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、利用中位數線性時間選擇

      中位數:是指將數據按大小順序排列起來,形成一個數列,居於數列中間位置的那個數據。

      算法思路:如果能在線性時間內找到一個划分基准使得按這個基准所划分出的2個子數組的長度都至少為原數組長度的ε倍(0<ε<1),那么就可以在最壞情況下用O(n)時間完成選擇任務。例如,當ε=9/10,算法遞歸調用所產生的子數組的長度至少縮短1/10。所以,在最壞情況下,算法所需的計算時間T(n)滿足遞推式T(n)<=T(9n/10)+O(n)。由此可得T(n)=O(n)

     實現步驟:

      (1)將所有的數n個以每5個划分為一組共組,將不足5個的那組忽略,然后用任意一種排序算法,因為只對5個數進行排序,所以任取一種排序法就可以了。將每組中的元素排好序再分別取每組的中位數,得到個中位數。

      (2)取這個中位數的中位數,如果是偶數,就找它的2個中位數中較大的一個作為划分基准。

      (3)將全部的數划分為兩個部分,小於基准的在左邊,大於等於基准的放右邊。在這種情況下找出的基准x至少比個元素大。因為在每一組中有2個元素小於本組的中位數,有個小於基准,中位數處於,即個中位數中又有個小於基准x。因此至少有個元素小於基准x。同理基准x也至少比個元素小。而當n≥75時≥n/4所以按此基准划分所得的2個子數組的長度都至少縮短1/4。

      

當然有的教科書上也會這樣:

算法分析:

 

 舉個例子說明:將54個元素分為下列組,每組5個,要找第15個小的元素

 然后將每組進行排序,得到如下:藍色是中位數數組

 

 現在求中位數數組的中位數,為節省空間,將中位數數組移到前兩列:

 然后將前兩列進行排序:

 知道28是中位數后,然后進行快速排序划分

 比較明顯,下面框框是比28小的元素

 由於15<28,應該在框框元素內找到第15小的元素,如下

 然后在上述元素中繼續划分5組,進行排序,排序結果如下:

 然后選出中位數數組的中位數是15

 然后繼續將所有元素快排,得到下面:

 然后比15小的14個,我們要找第15小的元素,就是這個數15,兩輪結束。

 注意:算法將每一組大小定為5,並取75作為是否作遞歸調用的分界點。當然除了5和75以外,還有其他選擇。

       中位數線性時間選擇程序清單如下:

//2-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);
	}
}

運行結果:

 

 參考:王曉東編注《算法設計與分析第二版》

           https://blog.csdn.net/liufeng_king/article/details/8480430?depth_1-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM