中位数和顺序统计(线性时间)算法导论9.2
来源:互联网 发布:淘宝预售权限开通 编辑:程序博客网 时间:2024/04/27 18:07
The general selection problem appears more difficult than the simple problem of finding a minimum. Yet, surprisingly, the asymptotic running time for both problems is the same:Θ(n). In this section, we present a divide-and-conquer algorithm for the selection problem. The algorithm RANDOMIZED-SELECT is modeled after the quicksort algorithm ofChapter 7. As in quicksort, the idea is to partition the input array recursively. But unlike quicksort, which recursively processes both sides of the partition, RANDOMIZED-SELECT only works on one side of the partition. This difference shows up in the analysis: whereas quicksort has an expected running time ofΘ(n lg n), the expected time of RANDOMIZED-SELECT is Θ(n).
RANDOMIZED-SELECT uses the procedure RANDOMIZED-PARTITION introduced inSection 7.3. Thus, like RANDOMIZED-QUICKSORT, it is a randomized algorithm, since its behavior is determined in part by the output of a random-numbergenerator. The following code for RANDOMIZED-SELECT returns theith smallest element of the array A[p .. r].
RANDOMIZED-SELECT(A, p, r, i)1 if p = r2 then return A[p]3 q ← RANDOMIZED-PARTITION(A, p, r)4 k ← q - p + 15 if i = k ▹ the pivot value is the answer6 then return A[q]7 elseif i < k8 then return RANDOMIZED-SELECT(A, p, q - 1, i)9 else return RANDOMIZED-SELECT(A, q + 1, r, i - k)
#include<iostream>#include<time.h>using namespace std;
void initArray(int arr[], int n, int top){ srand(time(NULL)); for(int i=0; i<n; i++){ arr[i] = rand()%top; }}
void printArray(int arr[], int n){ for(int i=0; i<n; i++){ cout<<arr[i]<<" "; } cout <<endl;}int partition(int arr[], int p, int r){ int temp = arr[r]; int i=p-1; for(int j=p; j<r;j++){ if(arr[j]<temp){ swap<int>(arr[++i],arr[j]); } } swap<int>(arr[++i],arr[r]); return i;}//return the index smallest number, counter from 0int selection(int arr[], int p, int r, int index){ if(p==r){ return arr[p]; } int q = partition(arr,p,r); if(index==q){ return arr[q]; }else if(index<q){ return selection(arr,p,q-1,index); }else{ return selection(arr,q+1,r,index-q-1); }
}void main(){ const int AD = 10; int arr[AD]; initArray(arr,AD,400); printArray(arr,AD); int smallest = selection(arr,0,AD-1,3); cout<<" 3 smallest: "<<smallest<<endl; cin.get();}
- 中位数和顺序统计(线性时间)算法导论9.2
- 算法导论-第九章-中位数和顺序统计量:最坏情况为线性时间的选择算法C++实现
- 算法导论:第9章 中位数和顺序统计量_1期望为线性时间的选择算法
- 算法导论:第9章 中位数和顺序统计量_2最坏情况为线性时间的选择算法
- 算法导论 第9章 中位数和顺序统计学(线性时间选择算法)
- 【算法导论】中位数和顺序统计量之选择算法
- 顺序统计量和中位数——线性时间的选择算法
- [算法导论]第九章《中位数和顺序统计量》
- 算法导论第九章-中位数和顺序统计量
- 算法导论笔记:09中位数和顺序统计量
- 中位数和顺序统计量---算法导论学习笔记
- 算法导论 第九章 中位数和顺序统计量
- 算法导论读书笔记(9)中位数和顺序统计量
- 《算法导论》读书笔记之第9章 中位数和顺序统计学 最坏情况是线性时间的选择算法
- 算法导论-------------中位数和顺序统计学
- 算法导论 第9章 中位数和顺序统计量 C++实现
- [算法导论 Ch9 中位数和顺序统计量] Selection in O(n)
- 算法导论中位数和顺序统计量之最大值最小值C#实现
- signal(SIGPIPE, SIG_IGN)
- list.add addAll
- snprintf
- android 自定义edittext边框
- 描述Oracle数据服务器启动方式
- 中位数和顺序统计(线性时间)算法导论9.2
- Clickonce在IE中立即启动提示:无法启动应用程序的解决方法!很简单! #远征OL##九州OL#
- Android中Parcel的解读
- 用来获取sd卡和系统/data分区的使用情况,包括:总的空间大小、可用空间、已用空间
- SRM 553 DIV2
- 点击后,变色,先前点击的不能恢复以前的颜色。
- android Toast大全(五种情形)建立属于你自己的Toast
- 在Mac OS X中配置Apache + PHP + MySQL
- cas单点注销后,返回指定地址(portal与CAS集成)