【分治法】線性時間選擇(轉)
轉自:http://blog.csdn.net/liufeng_king/article/details/8480430
線性時間選擇問題:給定線性序集中n個元素和一個整數k,1≤k≤n,要求找出這n個元素中第k小的元素,(這裏給定的線性集是無序的)。
1、隨機劃分線性選擇
線性時間選擇隨機劃分法可以模仿隨機化快速排序算法設計。基本思想是對輸入數組進行遞歸劃分,與快速排序不同的是,它只對劃分出的子數組之一進行遞歸處理。
程序清單如下:
1 //2d9-1 隨機劃分線性時間選擇 2 #include "stdafx.h" 3 #include <iostream> 4View Code#include <ctime> 5 using namespace std; 6 7 int a[] = {5,7,3,4,8,6,9,1,2}; 8 9 template <class Type> 10 void Swap(Type &x,Type &y); 11 12 inline int Random(int x, int y); 13 14 template <class Type> 15 int Partition(Type a[],int p,int r); 1617 template<class Type> 18 int RandomizedPartition(Type a[],int p,int r); 19 20 template <class Type> 21 Type RandomizedSelect(Type a[],int p,int r,int k); 22 23 int main() 24 { 25 for(int i=0; i<9; i++) 26 { 27 cout<<a[i]<<" "; 28} 29 cout<<endl; 30 cout<<RandomizedSelect(a,0,8,3)<<endl; 31 } 32 33 template <class Type> 34 void Swap(Type &x,Type &y) 35 { 36 Type temp = x; 37 x = y; 38 y = temp; 39 } 40 41 inline int Random(int x, int y) 42 { 43 srand((unsigned)time(0)); 44 int ran_num = rand() % (y - x) + x; 45 return ran_num; 46 } 47 48 template <class Type> 49 int Partition(Type a[],int p,int r) 50 { 51 int i = p,j = r + 1; 52 Type x = a[p]; 53 54 while(true) 55 { 56 while(a[++i]<x && i<r); 57 while(a[--j]>x); 58 if(i>=j) 59 { 60 break; 61 } 62 Swap(a[i],a[j]); 63 } 64 a[p] = a[j]; 65 a[j] = x; 66 return j; 67 } 68 69 template<class Type> 70 int RandomizedPartition(Type a[],int p,int r) 71 { 72 int i = Random(p,r); 73 Swap(a[i],a[p]); 74 return Partition(a,p,r); 75 } 76 77 template <class Type> 78 Type RandomizedSelect(Type a[],int p,int r,int k) 79 { 80 if(p == r) 81 { 82 return a[p]; 83 } 84 int i = RandomizedPartition(a,p,r); 85 int j = i - p + 1; 86 if(k <= j) 87 { 88 return RandomizedSelect(a,p,i,k); 89 } 90 else 91 { 92 //由於已知道子數組a[p:i]中的元素均小於要找的第k小元素 93 //因此,要找的a[p:r]中第k小元素是a[i+1:r]中第k-j小元素。 94 return RandomizedSelect(a,i+1,r,k-j); 95 } 96 }
程序解釋:利用隨機函數產生劃分基準,將數組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。
程序清單如下:
1 //2d9-2 中位數線性時間選擇 2 #include "stdafx.h" 3 #include <ctime> 4 #include <iostream> 5 using namespace std; 6 7 template <class Type> 8 void Swap(Type &x,Type &y); 9 10 inline int Random(int x, int y); 11 12 template <class Type> 13 void BubbleSort(Type a[],int p,int r); 14 15 template <class Type> 16 int Partition(Type a[],int p,int r,Type x); 17 18 template <class Type> 19 Type Select(Type a[],int p,int r,int k); 20 21 int main() 22 { 23 //初始化數組 24 int a[100]; 25 26 //必須放在循環體外面 27 srand((unsigned)time(0)); 28 29 for(int i=0; i<100; i++) 30 { 31 a[i] = Random(0,500); 32 cout<<"a["<<i<<"]:"<<a[i]<<" "; 33 } 34 cout<<endl; 35 36 cout<<"第83小元素是"<<Select(a,0,99,83)<<endl; 37 38 //重新排序,對比結果 39 BubbleSort(a,0,99); 40 41 for(int i=0; i<100; i++) 42 { 43 cout<<"a["<<i<<"]:"<<a[i]<<" "; 44 } 45 cout<<endl; 46 } 47 48 template <class Type> 49 void Swap(Type &x,Type &y) 50 { 51 Type temp = x; 52 x = y; 53 y = temp; 54 } 55 56 inline int Random(int x, int y) 57 { 58 int ran_num = rand() % (y - x) + x; 59 return ran_num; 60 } 61 62 //冒泡排序 63 template <class Type> 64 void BubbleSort(Type a[],int p,int r) 65 { 66 //記錄一次遍歷中是否有元素的交換 67 bool exchange; 68 for(int i=p; i<=r-1;i++) 69 { 70 exchange = false ; 71 for(int j=i+1; j<=r; j++) 72 { 73 if(a[j]<a[j-1]) 74 { 75 Swap(a[j],a[j-1]); 76 exchange = true; 77 } 78 } 79 //如果這次遍歷沒有元素的交換,那麽排序結束 80 if(false == exchange) 81 { 82 break ; 83 } 84 } 85 } 86 87 template <class Type> 88 int Partition(Type a[],int p,int r,Type x) 89 { 90 int i = p-1,j = r + 1; 91 92 while(true) 93 { 94 while(a[++i]<x && i<r); 95 while(a[--j]>x); 96 if(i>=j) 97 { 98 break; 99 } 100 Swap(a[i],a[j]); 101 } 102 return j; 103 } 104 105 106 template <class Type> 107 Type Select(Type a[],int p,int r,int k) 108 { 109 if(r-p<75) 110 { 111 BubbleSort(a,p,r); 112 return a[p+k-1]; 113 } 114 //(r-p-4)/5相當於n-5 115 for(int i=0; i<=(r-p-4)/5; i++) 116 { 117 //將元素每5個分成一組,分別排序,並將該組中位數與a[p+i]交換位置 118 //使所有中位數都排列在數組最左側,以便進一步查找中位數的中位數 119 BubbleSort(a,p+5*i,p+5*i+4); 120 Swap(a[p+5*i+2],a[p+i]); 121 } 122 //找中位數的中位數 123 Type x = Select(a,p,p+(r-p-4)/5,(r-p-4)/10); 124 int i = Partition(a,p,r,x); 125 int j = i-p+1; 126 if(k<=j) 127 { 128 return Select(a,p,i,k); 129 } 130 else 131 { 132 return Select(a,i+1,r,k-j); 133 } 134 }View Code
運行結果如下:
【分治法】線性時間選擇(轉)