一亿数字取前N个

论坛上看到,记录以备不时之需

链接:http://bbs.csdn.net/topics/390034686


package main;

import java.util.Arrays;
import java.util.Date;
import java.util.Random;

/**
 * 算法原理:
 *    把一亿个数字的前100个 首先放入数组。 然后把最小值放在ary[0]。
 *    然后再,循环100到一亿 之间的。 每次循环判断当前数字是否大于ary[0]
 *  当大于时,当前数字放入ary[0] 并再次重构数组最小值进入ary[0]  以此类推 。
 *  当循环完这一亿个数字后。 最大的前100个数字就出来了。
 * @author Administrator
 *
 */
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;  
                }  
            }  
        }  
    }

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值