Finding the first n largest elements in an array

后端 未结 8 438
不思量自难忘°
不思量自难忘° 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:33
    //finding the bigest number in the array//
    
    double big = x[0];
    for(t=0;t<x[t];t++)
    {
        if(x[t]>big)
        {
            big=x[t];
        }
    }
    printf("\nThe bigest number is    %0.2lf  \n",big);
    
    0 讨论(0)
  • 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<array.length;i++){                                 
                        System.out.print(array[i]+" ");                          
                }                                                                
                System.out.print("] \n");                                        
        }  
        public static int getMax(){
                int max=array[0];
                array[0]=array[heapsize];
                maxHeapify(array,0,--heapsize);
        }
    
     }                                                                                                                                                             
    
    0 讨论(0)
提交回复
热议问题