Skip to content

Commit

Permalink
randomized-select
Browse files Browse the repository at this point in the history
  • Loading branch information
gzc authored Mar 12, 2021
1 parent f25a07e commit b7d3df5
Showing 1 changed file with 38 additions and 57 deletions.
95 changes: 38 additions & 57 deletions C09-Medians-and-Order-Statistics/randomized-select.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,47 @@
/*************************************************************************
> File Name: randomized-select.cpp
> Author: Louis1992
> Mail: [email protected]
> Blog: http://gzc.github.io
> Created Time: Sun May 24 11:46:39 2015
************************************************************************/
#include<iostream>
#include<vector>
using namespace std;

class Solution {

int kthSmallest(vector<int>& arr, int l, int r, int k)
{

if (k > 0 && k <= r - l + 1)
{
int pos = randomPartition(arr, l, r);

if (pos-l == k-1)
return arr[pos];
else if(pos-l > k-1)
return kthSmallest(arr, l, pos-1, k);
else
return kthSmallest(arr, pos+1, r, k-pos+l-1);
}
return INT_MAX;
}

int partition(vector<int>& arr, int l, int r)
{
int x = arr[r], i = l;
for(int j = l; j <= r - 1; j++)
{
if (arr[j] <= x)
{
swap(arr[i], arr[j]);
i++;
}
int partition(vector<int>& arr, int l, int r) {
int x = arr[r], i = l;
for (int j = l; j <= r - 1; j++) {
if (arr[j] <= x) {
swap(arr[i], arr[j]);
i++;
}
swap(arr[i], arr[r]);
return i;
}

int randomPartition(vector<int>& arr, int l, int r)
{
int n = r-l+1;
int pivot = rand() % n;
swap(arr[l + pivot], arr[r]);
return partition(arr, l, r);
}
swap(arr[i], arr[r]);
return i;
}

int randomPartition(vector<int>& arr, int l, int r) {
int n = r - l + 1;
int pivot = rand() % n;
swap(arr[l + pivot], arr[r]);
return partition(arr, l, r);
}

public:
int findKthLargest(vector<int>& nums, int k) {
return kthSmallest(nums, 0, nums.size()-1, nums.size()-k+1);
int kthSmallest(vector<int>& arr, int l, int r, int k) {
int pos = randomPartition(arr, l, r);
if (pos - l == k - 1) {
return arr[pos];
} else if (pos - l > k - 1) {
return kthSmallest(arr, l, pos - 1, k);
} else {
return kthSmallest(arr, pos+1, r, k-pos+l-1);
}
};
}

int kthSmallest(vector<int>& arr, int k) {
// TODO: Validate k is in valid range.
return kthSmallest(arr, 0, arr.size() - 1, k);
}

int main()
{
int arr[5] = {2,4,0,-2,8};
vector<int>v(arr, arr+5);
Solution s;
cout << s.findKthLargest(v, 2) << endl;
}
int main() {
vector<int>v{3,4,2,5,1};
cout << kthSmallest(v, 1) << endl;
cout << kthSmallest(v, 2) << endl;
cout << kthSmallest(v, 3) << endl;
cout << kthSmallest(v, 4) << endl;
cout << kthSmallest(v, 5) << endl;
}

0 comments on commit b7d3df5

Please sign in to comment.