Quick Sort Concept


  • 0
    P
    class Solution {
        
        int[] arr;
        public int findKthLargest(int[] nums, int k) {
            sort(nums,0,nums.length-1);
            for (int i=0;i<nums.length;i++){
                System.out.println(nums[i]);
            }
            
            return nums[nums.length-k];                        
        }
        
        public void sort (int[] ar, int left, int right){
                            
            if (left<right){
               
                int center = findPivot(ar,left,right);            
                sort(ar,left,center-1);
                sort(ar,center+1,right);        
            }                 
        }
        
        public int findPivot(int[] ar,int left, int right){   
                    
            int pivot = ar[right];
            int i = left-1;
            for (int j=left;j<right;j++){            
                if (ar[j]<=pivot){
                    
                    i++;                
                    int temp=ar[j];
                    ar[j]=ar[i];
                    ar[i]=temp;                                
                }            
            }
            
            int temp = ar[i+1];        
            ar[i+1]=ar[right];
            ar[right]=temp;        
            return i+1;    
            
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.