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;  
                }  
            }  
        }  
    }

}
最近下载更多
lironggang  LV38 2023年3月26日
lucky_out3  LV1 2021年10月25日
qqyouxiang2019  LV2 2021年5月24日
周晓龙  LV2 2021年4月14日
guizhen080616  LV1 2020年11月23日
RinkaOvO  LV2 2020年4月24日
15232583526  LV1 2019年8月7日
wyxst1314  LV1 2019年6月14日
2224947710  LV17 2019年6月7日
yangctz  LV24 2018年12月3日
最近浏览更多
uni-code_0123  LV1 2022年12月27日
luozf1990  LV3 2022年1月6日
qqyouxiang2019  LV2 2021年5月24日
lironggang  LV38 2021年4月2日
邈话12123  LV9 2020年8月8日
飞翔的天空 2020年6月28日
暂无贡献等级
cclovecoding 2020年5月9日
暂无贡献等级
RinkaOvO  LV2 2020年4月24日
yongqiang347 2020年1月9日
暂无贡献等级
adfa5555 2020年1月4日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友