從海量資料中尋找出topK的最優演算法代碼

來源:互聯網
上載者:User

標籤:int   out   演算法   exception   匯入   pow   tmp   heap   ase   

package findMinNumIncludedTopN;
/**
 * 小頂堆
 * @author TongXueQiang
 * @date 2016/03/09
 * @since JDK 1.8
 */
public class MinHeap {
 int[] heap;
 int heapsize;

 public MinHeap(int[] array) {
  this.heap = array;
  this.heapsize = heap.length;
 }
 
 /**
  * 構建小頂堆
  */
 public void BuildMinHeap() {
  for (int i = heapsize / 2 - 1; i >= 0; i--) {
   Minify(i);// 依次向上將當前子樹最大堆化
  }
 }
 
 /**
  * 堆排序
  */
 public void HeapSort() {
  for (int i = 0; i < heap.length; i++) {
   // 執行n次,將每個當前最大的值放到堆末尾
   swap(heap,0,heapsize-1);   
   heapsize--;
   Minify(0);
  }
 }
 
 /**
  * 對非分葉節點調整
  * @param i
  */
 public void Minify(int i) {
  int l = 2*i + 1;
  int r = 2*i + 2;
  int min;

  if (l < heapsize && heap[l] < heap[i])
   min = l;
  else
   min = i;
  if (r < heapsize && heap[r] < heap[min])
   min = r;
  if (min == i || min >= heapsize)// 如果largest等於i說明i是最大元素
            // largest超出heap範圍說明不存在比i節點大的子女
   return;
  swap(heap,i,min);
  Minify(min); 
 }

 private void swap(int[] heap, int i, int min) {
  int tmp = heap[i];// 交換i與largest對應的元素位置,在largest位置遞迴調用maxify
  heap[i] = heap[min];
  heap[min] = tmp;  
 }

 public void IncreaseValue(int i, int val) {
  heap[i] = val;
  if (i >= heapsize || i <= 0 || heap[i] >= val)
   return;
  int p = Parent(i);
  if (heap[p] >= val)
   return;
  heap[i] = heap[p];
  IncreaseValue(p, val);
 }

 private int Parent(int i) {
  return (i - 1) / 2;
 }
}

 

package findMinNumIncludedTopN;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * 從海量資料中尋找出前k個最大值,精確時間複雜度為:k + (n - k) * lgk,空間複雜度為 O(k),目前為所有演算法中最優演算法
 *
 * @author TongXueQiang
 * @date 2016/03/08
 * @since JDK 1.8
 */
public class FindMinNumIncluedTopN {
 /**
  * 從海量資料中尋找出前k個最大值
  *
  * @param k
  * @return
  * @throws IOException
  */
 public int[] findMinNumIncluedTopN(int k) throws IOException {
  Long start = System.nanoTime();
  
  int[] array = new int[k];
  int index = 0;
  // 從檔案匯入海量資料
  BufferedReader reader = new BufferedReader(new FileReader(new File("F:/number.txt")));
  String text = null;
  // 先讀出前n條資料,構建堆
  do {
   text = reader.readLine();
   if (text != null) {
    array[index++] = Integer.parseInt(text);
   }   
  } while (text != null && index <= k - 1);
  
  MinHeap heap = new MinHeap(array);//初始化堆
  for (int i : heap.heap) {
   System.out.print(i + " ");
  }
  
  heap.BuildMinHeap();//構建小頂堆
  System.out.println();
  System.out.println("構建小頂堆之後:");
  for (int i : heap.heap) {
   System.out.print(i + " ");
  }
  System.out.println();
  // 遍曆檔案中剩餘的n(檔案資料容量,假設為無限大)-k條資料,如果讀到的資料比heap[0]大,就替換之,同時更新堆
  while (text != null) {
   text = reader.readLine();
   if (text != null && !"".equals(text.trim())) {
    if (Integer.parseInt(text) > heap.heap[0]) {
     heap.heap[0] = Integer.parseInt(text);
     heap.Minify(0);//調整小頂堆
    }
   }
  }
  //最後對堆進行排序(預設降序)
  heap.HeapSort();
  
  Long end = System.nanoTime();
  double time = (end - start) / Math.pow(10,9);
  System.out.println("用時:"+ time + "秒");
  for (int i : heap.heap) {
   System.out.println(i);
  }
  return heap.heap;
 }
}

從海量資料中尋找出topK的最優演算法代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.