Quick select is one of very popular algorithm used to find k'th largest element in a list. It can be use to find median or finding k'th smallest element etc.
Finding K'th largest element looks easy , we just need to sort & get K'th largest . But here we are dealing with time so we need fastest way to do this
So basically we have to do is find a pivot that divide array in 2 group ,left group contain all element less then pivot and right group contain all element greater than pivot .
So if we want 3rd largest element then pivot index will be (n-3) and data at that index is third largest data .
If you did not Understand than probability you don't know Quick sort . I strongly Recommend first to understand Quick sort. Even though you did't get it then follow our code .
Finding K'th largest element looks easy , we just need to sort & get K'th largest . But here we are dealing with time so we need fastest way to do this
Time complexity of Quick Select (average case) =O(n)Quick Select is basically modified version of Quick sort. In Quick Sort we need to sort all element but Quick select just want to know pivot value that divide array in given ratio.
Quick sortlet a example we have 10 different number in array & we want to find 3rd largest element.(we assume 0..9 index array) then in sorted array its position should be (10-3)th Index.
So basically we have to do is find a pivot that divide array in 2 group ,left group contain all element less then pivot and right group contain all element greater than pivot .
So if we want 3rd largest element then pivot index will be (n-3) and data at that index is third largest data .
If you did not Understand than probability you don't know Quick sort . I strongly Recommend first to understand Quick sort. Even though you did't get it then follow our code .
Quick Select -Find K'th largest element
- //Quick Selelct
- //very useful for
- //Find kth largest or smallest element
- //find median
- //O(n)
- //this program demonstrate to find K'th largest from given list
- //assuming all are different numbers
- #include<iostream>
- #include<vector>
- using namespace std;
- int Quick_Select(vector<int> &arr,int p,int r,int index_of_kth_largest);
- int partition(vector<int> &arr,int p,int r,int pivot);
- int main(){
- vector<int> arr;
- int n,k;
- cout<<"Enter No of element (n) :";
- cin>>n;
- cout<<"Enter Value of k :";
- cin>>k;
- cout<<"Enter array element :"<<endl;
- int key;
- for(int i=0;i<n;i++){
- cin>>key;
- arr.push_back(key);
- }
- int kth=Quick_Select(arr,0,n-1,n-k);
- cout<<"K'th largest number is "<<kth;
- return 0;
- }
- int Quick_Select(vector<int> &arr,int p,int r,int index_of_kth_largest){
- //We are basically finding situation when pivote == index_of_kth_largest
- //then all elemnt less than that is on left side and all element greter than that is in right side
- if(index_of_kth_largest==r)
- return arr[r];
- int pivotI;
- while(1){
- pivotI=(p+r)/2;
- pivotI=partition(arr,p,r,pivotI);
- if(pivotI== index_of_kth_largest)
- return arr[pivotI];
- //make sure to chhose proper sub arr where index=index_of_kth_largest reside
- if(pivotI <index_of_kth_largest)
- p=pivotI+1;
- if(pivotI>index_of_kth_largest)
- r=pivotI-1;
- }
- }
- int partition(vector<int> &arr,int p,int r,int pivotIndex){
- int i=p-1;
- //swap and move pivote data to end
- int temp;
- int pivot=arr[pivotIndex];
- temp=arr[pivotIndex];
- arr[pivotIndex]=arr[r];
- arr[r]=temp;
- //arrange arr such that all elemnt below pivote come to left side of pivotIndex
- //and all elemnt greter than pivot come right side of pivotIndex
- for(int j=p;j<r;j++){
- if(pivot<=arr[j]){
- i++;
- temp=arr[i];
- arr[i]=arr[j];
- arr[j]=temp;
- }
- }
- i++;
- temp=arr[i];
- arr[i]=arr[r];
- arr[r]=temp;
- return i;
- }
I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Data Science with Python , kindly contact us http://www.maxmunus.com/contact
ReplyDeleteMaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us.
Sangita Mohanty
MaxMunus
E-mail: sangita@maxmunus.com
Skype id: training_maxmunus
Ph:(0) 9738075708 / 080 - 41103383
http://www.maxmunus.com/