import java
import java
import java
public class Top
public static void main(String[] args) {
find()
}
public static void find( ) {//
int number =
int maxnum =
int i =
int topnum =
Date startTime = new Date()
Random random = new Random()
int[] top = new int[topnum];
for (i =
top[i] = Math
// top[i] = getNum(i)
}
buildHeap(top
for (i = topnum; i < number; i++) {
int currentNumber
// int currentNumber
// 大於 top[
if (top[
top[
shift(top
}
}
System
sort(top)
System
Date endTime = new Date()
System
}
public static int getNum(int i){
return i;
}
//構造排序數組
public static void buildHeap(int[] array
int pos = (len
for (int i = pos; i >=
shift(array
}
}
/**
* @param array top數組
* @param from 開始
* @param len 數組長度
* @param pos 當前節點index
* */
public static void shift(int[] array
// 保存該節點的值
int tmp = array[from + pos];
int index = pos *
while (index < len)// 存在左節點
{
if (index +
&& array[from + index] > array[from + index +
{
// 如果右邊節點比左邊節點小
index +=
}
if (tmp > array[from + index]) {
array[from + pos] = array[from + index];
pos = index;
index = pos *
} else {
break;
}
}
// 最終全部置換完畢後
array[from + pos] = tmp;
}
public static void sort(int[] array){
for(int i =
//當前值當作最小值
int min = array[i];
for(int j = i+
if(min>array[j]){
//如果後面有比min值還小的就交換
min = array[j];
array[j] = array[i];
array[i] = min;
}
}
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25628.html