1億個數中找出最小的100個數--最小堆


版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/jj12345jj198999/article/details/17169559
package com.my.util;
 
import java.util.Arrays;
import java.util.Date;
import java.util.Random;
 
public class Top100 {
    
    public static void main(String[] args) {
        find();
    }
    public static void find( ) {//
        int number = 100000000;// 一億個數
        int maxnum = 1000000000;// 隨機數最大值
        int i = 0;
        int topnum = 100;// 取最大的多少個
     
        Date startTime = new Date();
        
        Random random = new Random();
        int[] top = new int[topnum];
        for (i = 0; i < topnum; i++) {
            top[i] = Math.abs(random.nextInt(maxnum));//設置為隨機數
//            top[i] = getNum(i);
        }
 
        buildHeap(top, 0, top.length);// 構建最小堆, top[0]為最小元素
        for (i = topnum; i < number; i++) {
 
            int currentNumber2 = Math.abs(random.nextInt(maxnum));//設置為隨機數
//            int currentNumber2 = getNum(i);
            // 大於 top[0]則交換currentNumber2  重構最小堆
            if (top[0] < currentNumber2) {
                top[0] = currentNumber2;
                shift(top, 0, top.length, 0); // 構建最小堆 top[0]為最小元素
            }
        }
        System.out.println(Arrays.toString(top));
        sort(top);
        System.out.println(Arrays.toString(top));
        
        Date endTime = new Date();
        System.out.println("用了"+(endTime.getTime() - startTime.getTime())+"毫秒");
 
    }
    
    public static int getNum(int i){
        return i;
    }
 
 
    //構造排序數組
    public static void buildHeap(int[] array, int from, int len) {
        int pos = (len - 1) / 2;
        for (int i = pos; i >= 0; i--) {
            shift(array, from, len, i);
        }
    }
 
    /**
     * @param array top數組
     * @param from 開始
     * @param len 數組長度
     * @param pos 當前節點index
     * */
    public static void shift(int[] array, int from, int len, int pos) {
        // 保存該節點的值 
        int tmp = array[from + pos];
 
        int index = pos * 2 + 1;// 得到當前pos節點的左節點
        while (index < len)//  存在左節點
        {
            if (index + 1 < len
                    && array[from + index] > array[from + index + 1])// 如果存在右節點
            {
                // 如果右邊節點比左邊節點小,就和右邊的比較
                index += 1;
            }
             
            if (tmp > array[from + index]) {
                array[from + pos] = array[from + index];
                pos = index;
                index = pos * 2 + 1;
            } else {
                break;
            }
        }
        // 最終全部置換完畢后 ,把臨時變量賦給最后的節點
        array[from + pos] = tmp;
    }
    
    public static void sort(int[] array){  
        for(int i = 0; i < array.length - 1; i++){  
            //當前值當作最小值  
            int min = array[i];  
            for(int j = i+1; j < array.length; j++){  
                if(min>array[j]){  
                    //如果后面有比min值還小的就交換  
                    min = array[j];  
                    array[j] = array[i];  
                    array[i] = min;  
                }  
            }  
        }  
    }
 
}

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM