Finding the first n largest elements in an array

后端 未结 8 461
不思量自难忘°
不思量自难忘° 2020-12-09 04:42

I have got an array containing unique elements. I need to find out the first n largest elements in the array in the least complexity possible. The solution that I could thin

8条回答
  •  孤城傲影
    2020-12-09 05:41

    I don't believe on this but you could also create a heap out of it in O(n). And then just remove the root k number of times and heapify the heap for k largest numbers. In this way for each largest numbers it will cost you log(n).

    public class HeapSort1{                                                          
        public static void main(String args[]){                                  
                int[] array={5,75,1,5,4,1,2,4,8,4,2,15,4,2,1,5,779,9,1};         
                int heapsize=array.length-1;                                     
                for(int i=heapsize/2;i>=0;i--){                                  
                        maxHeapify(array,i,heapsize);                            
                }                                                                
                for(int i=heapsize;i>0;i--){                                     
                        array[i]=array[0]+array[i];                              
                        array[0]=array[i]-array[0];                              
                        array[i]=array[i]-array[0];                              
                        maxHeapify(array,0,--heapsize);                          
                }                                                                
                printArray(array);                                               
        }                                                                        
        public static void maxHeapify(int[] array,int i,int heapsize){           
                int largest=i;                                                   
                int left=2*i+1;                                                  
                int right=2*i+2;                                                 
                if(left<=heapsize && array[left]>array[i]){                      
                        largest=left;                                            
                }                                                                
                if(right<=heapsize && array[right]>array[largest]){              
                        largest=right;                                           
                }                                                                
                if(largest!=i){                                                  
                        array[i]=array[largest]+array[i];                        
                        array[largest]=array[i]-array[largest];                  
                        array[i]=array[i]-array[largest];                        
                        maxHeapify(array,largest,heapsize);                      
                }                                                                
        }                                                                        
        public static void printArray(int[] array){                              
                System.out.print("\n [");                                        
                for(int i=0;i

提交回复
热议问题