Random Selection

Random Selection Algorithm

This is the implementation of Random Selection Algorithm as instructed in Algorithms: Design and Analysis, Part1, Coursera. But I think there may be something wrong in his slides.

1. Analysis of the pseudo-code

The pseudo-code he gives is as follows:

RSelect (array A, length n, order statistics i)

  - if n = 1, return A[1]

  - choose pivot p from A uniformly at random

  - partition A around p, let j = new index of p

  - if j = i, return p

  -if j > i, return RSelect(1st part of A, j-1, i)

  -else if j < i, return RSelect(2nd part of A, n-j, i-j)

It should be noted that j is index of pivot point as opposed to the original array. Therefore, the last line here should be modified. Let me use an example to illustrate my point. Note that in this example, index of an array starts at 1.

Let A = {5,7,1,4,2}, suppose we want to pick out the 4th statistics (j=4), which is 5 in our case. In the first round, suppose 2 is chosen as the pivot. After PartitionAroundPivot subroutinem the array would be A = {1,2,5,7,4}, where j = 2. Therefore, we should recursively call RSelect on the second half of the array and look for the 2nd statistics, which should be computed as j - j =2, correct. But this set j to 2. On the second round ,suppose we choose 4 as pivot, then the subarray should be sub-A = {4,5,7}, with j = 3, and now we should call RSelect on the second half, and look for the 1st order statistics. BUT, j - j = 2 - 3 = -1. Problem has arose. Problem is, j does not denote the index of pivot in the subarray, while j does denote the statistics in new subarray, so the comparison between j and j is meaningless. We should therefore modify j to j - j, where j means the left boundary of the subarray.

Another possible explanation may be that ‘new index‘ means index in the ‘new‘ subarray. But this may cause trouble to return the final result.

2. My Implementation

The code will generate a random array and select 1st statistics to nth statistics sequentially, which equals a sorting program. Another things worth noticing is that array index in C starts at 0. So we should increment pivot index by one when comparing with order statistics, where the incremented pivot index itself becomes an order statistics. And also, I am kind of rusty when dealing with random number generation. I have learned it in freshmen year but kind of forget. So maybe someone can give me a link or something to a concise tutorial?

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 14
 4 int ChoosePivot(int l, int r){
 5     srand(time(NULL));
 6     int randNum = rand();
 7     int pivotIndex;
 8     pivotIndex = randNum %(r-l+1);
 9     pivotIndex = pivotIndex + l;
10     return pivotIndex;
11 }
12
13 int PartitionAroundPivot(int* A, int pivotIndex, int l, int r){
14     int i,j;
15     int pivot = A[pivotIndex], temp;
16
17     i = l + 1;
18     j = l;
19     //swap pivot to the first one
20     if (pivotIndex != l){
21         temp = A[l]; A[l] = A[pivotIndex]; A[pivotIndex] = temp;
22     }
23     for (j = l + 1; j < r + 1; j++){
24         if (A[j] < pivot) {
25             temp = A[i]; A[i] = A[j]; A[j] = temp;
26             i++;
27         }
28     }
29     temp = A[i-1]; A[i-1] = pivot; A[l] = temp;
30     return i-1;
31
32 }
33
34 //l and r specifies the sub-array, i specifies the pivot
35 int RSelect(int* A, int l, int r, int i){
36     int p, p_th;
37     if (r-l+1 == 1)
38         return A[l];
39     p = ChoosePivot(l,r);
40     p_th = PartitionAroundPivot(A,p,l,r);
41
42     if (p_th-l+1==i)
43         return A[p_th];
44     if (p_th-l+1>i)
45         return RSelect(A,l,p_th-1,i);
46     if (p_th-l+1<i)
47         return RSelect(A,p_th+1,r,i-p_th+l-1);
48 }
49
50 int main()
51 {
52     int A[N];
53     int r,i;
54     srand(time(NULL));
55     for (i=0;i<N;i++){
56         A[i] = rand();
57     }
58     for (i=1;i<N+1;i++){
59         r = RSelect(A,0,N-1,i);
60         printf("%d\n",r);
61     }
62
63     return 0;
64 }
时间: 2024-10-12 07:09:48

Random Selection的相关文章

(转) Deep Learning in a Nutshell: Reinforcement Learning

Deep Learning in a Nutshell: Reinforcement Learning Share: Posted on September 8, 2016by Tim Dettmers No CommentsTagged Deep Learning, Deep Neural Networks, Machine Learning,Reinforcement Learning This post is Part 4 of the Deep Learning in a Nutshel

组会准备

LSTM Networks for Sentiment Analysis Summary This tutorial aims to provide an example of how a Recurrent Neural Network (RNN) using the Long Short Term Memory (LSTM) architecture can be implemented using Theano. In this tutorial, this model is used t

Q-learning 算法学习-1

从下面的网站进行学习. http://mnemstudio.org/path-finding-q-learning-tutorial.htm his tutorial introduces the concept of Q-learning through a simple but comprehensive numerical example.  The example describes an agent which uses unsupervised training to learn a

[原][译][osgearth]样式表style中参数总结(OE官方文档翻译)

几何Geometry 高度Altitude 挤压Extrusion 图标Icon 模型Model 渲染Render 皮肤Skin 文本Text 覆盖Coverage 提示: 在SDK中,样式表的命名空间是osgEarth::Symbology 每个符号类是在AltitudeSymbol中,属性通过LineSymbol::strokeWidth() 访问器可用 值类型 float: 实数 float with units: 有单位的实数, e.g. 20px (20 pixels) or 10m

Dr.memory

Run Dr.memory on visual c++ 2013 Title: Dr. Memory Command: C:\Program Files (x86)\Dr. Memory\bin\drmemory.exe Arguments: -visual_studio -- $(TargetPath) Initial Directory: $(TargetDir) set arguments-light -no_midchunk_inheritance_ok -no_check_gdi -n

PYTHON-进阶-ITERTOOLS模块

PYTHON-进阶-ITERTOOLS模块小结 这货很强大, 必须掌握 文档 链接 pymotw 链接 基本是基于文档的翻译和补充,相当于翻译了 itertools用于高效循环的迭代函数集合 组成 总体,整体了解 无限迭代器 迭代器 参数 结果 例子 count() start, [step] start, start+step, start+2*step, ... count(10) --> 10 11 12 13 14 ... cycle() p p0, p1, ... plast, p0,

【DeepLearning】Exercise:PCA and Whitening

Exercise:PCA and Whitening 习题链接:Exercise:PCA and Whitening pca_gen.m %%================================================================ %% Step 0a: Load data % Here we provide the code to load natural image data into x. % x will be a 144 * 10000 matr

PCA and Whitening on natural images

Step 0: Prepare data Step 0a: Load data The starter code contains code to load a set of natural images and sample 12x12 patches from them. The raw patches will look something like this: These patches are stored as column vectors in the matrix x. Step

2017-7-18-每日博客-关于Linux下的鲜为人知的10条命令.doc

这篇文章的目的是介绍一些少有人知的Linux命令,它们一定会高效地帮你管理你的桌面/服务器. 1. sudo !!命令 没有特定输入sudo命令而运行,将给出没有权限的错误.那么,你不需要重写整个命令,仅仅输入'!!'就可以抓取最后的命令. $ apt-get update E: Could not open lock file /var/lib/apt/lists/lock - open(13: Permission denied) E: Unable to lock directory /v