int randomized_select(int *arr, int left, int right, int i) { if (left == right) return arr[left]; int begin = left; int end = right; int key = arr[begin]; while (begin < end) { while (begin < end && arr[end] >= key) end--; arr[begin] = arr[end]; while (begin < end && arr[begin] <= key) begin++; arr[end] = arr[begin]; } arr[begin] = key; int n = begin - left + 1; if (n == i) return arr[begin]; else if (n < i) { return randomized_select(arr, begin+1, right, i - n); } else return randomized_select(arr, left, begin - 1, i); }
int randomized_select(int* arr, int s, int e, int i) { if (s == e) return arr[s]; int p = randomized_partition(arr, s, e); int k = p-s+1; // +1 because, indexing starts at 0 while i starts at 1 if (k == i) return arr[p]; else if (i < k) return randomized_select(arr, s, p-1, i); // if it lies to the left, the required index is still i else return randomized_select(arr, p+1, e, i-k); // if it lies to the right, the required index is (i - k) }
int randomized_select(int *arr, int start, int end, int i) { int randomized_partition(int*, int, int); int partition = randomized_partition(arr, start, end); int k = partition - start + 1; if(k == i) return arr[partition]; else { if(k < i) return randomized_select(arr, partition+1, end, i-k); else return randomized_select(arr, start, partition-1, i); } }
// MARK: selection kth smallest element of the array A[p,r), k[0,1,...] [q,t) int randomized_select(int A[], int p, int r, int k) { pivot_t pivot, span; if (p == r-1) { return A[p]; } pivot = randomized_partition_equal(A, p, r); span.q = pivot.q - p; span.t = pivot.t - p; if (k >= span.q && k < span.t) { return A[pivot.q]; } else if (k >= span.t) { return randomized_select(A, pivot.t, r, k-span.t); } else { return randomized_select(A, p, pivot.q, k); } } //expected not worst ø(n) assuming that the elements are distinct, but here the elements can be equal because of pivot_t
double randomized_select(double *a, int p, int r, int i) { int q, k; if (p == r) return a[p]; q = randomized_partition(a, p, r); /* number of elements in the first list */ k = q - p + 1; if (i <= k) return randomized_select(a, p, q, i); else return randomized_select(a, q+1, r, i-k); }
int main(int argc, char const *argv[]) { int arr[N] = {3, 4, 1, 7, 5, 8, 6}; int i = 4; // ith smallest element int x = randomized_select(arr, 0, N-1, i); printf("element #%d: %d\n", i, x); return 0; }
int randomized_select(vector<int>& nums,int p,int r, int i) { if(p==r) { return nums[p]; } int x=nums[r], l=p; for(int j=p; j<r; j++) { if(nums[j]>x) { swap(nums[l++],nums[j]); } } swap(nums[l],nums[r]); int q=l; int k=q-p+1; if(i==k) return nums[q]; else if(i<k) return randomized_select(nums,p,q-1,i); else return randomized_select(nums,q+1,r,i-k); }
int median(int A[], int n) { return randomized_select(A, n, (1+n)/2); }
int main(void) { int arr[10] = {23, 34, 21, 67, 89, 90, 6, 31, 16, 54}; printf("%d \n", randomized_select(arr, 0, 9, 5)); return 0; }
// method 1 use quicksort partition int findKthLargest(vector<int>& nums, int k) { return randomized_select(nums,0,nums.size()-1,k); }