算法兴趣----- 一亿数据获取前100个最大值(仅供参考,基于快速排序的实现时间不稳定,基于最小堆实现。如果我们只要求前K个最大(小)值的时候,用堆是最好的选择,因为这里不用每次都排序了)

文章来源:http://blog.csdn.net/beiyeqingteng/article/details/7534489

前言:

刚刚在CSDN上看到一个网友利用最小堆实现 “ 获取一亿数据获取前100个最大值” 。原帖请看:http://blog.csdn.net/yjflinchong/article/details/7533972。 然后自己利用quicksort的原理也写了一个程序来解决那个问题。通过测试,基于quicksort原理的方法平均运行时间是1.264秒,基于最小堆方法的平均运行时间是0.288秒 (网友写的程序运行时间比我的大很多,0.288秒这个程序是我自己写的,如果测试网友写的基于minHeap的方法,运行时间是2.501秒)。基于最小堆方法运行时间很稳定(每次运行时间相差很小),基于quicksort原理的方法运行时间不稳定(每次运行时间相差大)。

基于quicksort实现的原理如下:

1. 假设数组为 array[N] (N = 1 亿),首先利用quicksort的原理把array分成两个部分,左边部分比 array[N - 1] (array中的最后一个值,即pivot) 大, 右边部分比pivot 小。然后,可以得到 array[array.length - 1] (即 pivot) 在整个数组中的位置,假设是 k.
2. 如果 k 比 99 大,我们在数组[0, k - 1]里找前 100 最大值。 (继续递归)
3. 如果 k 比 99 小, 我们在数组[k + 1, ..., N ]里找前 100 - (k + 1) 最大值。(继续递归)
4. 如果 k == 99, 那么数组的前 100 个值一定是最大的。(退出)

代码如下:

[java]  view plain copy
  1. public class TopHundredQuickSort {  
  2.       
  3.     public void tophundred(int[] array, int start, int end, int k) {  
  4.           
  5.         int switchPointer = start;  
  6.         int pivot = array[end]; //array最后一个值作为pivot  
  7.         for (int i = start; i < end; i++) {  
  8.             if (array[i] >= pivot) {  
  9.                 swap(array, switchPointer, i);  
  10.                 switchPointer++;  
  11.             }  
  12.         }  
  13.         swap(array, end, switchPointer);//交换后,array左边的值比pivot大,右边的值比pivot小  
  14.           
  15.         if (switchPointer < k - 1) {  
  16.             tophundred(array, switchPointer + 1, end, k);  
  17.         } else if (switchPointer == k - 1) {  
  18.             return;  
  19.         } else {  
  20.             tophundred(array, 0, switchPointer - 1, k);  
  21.         }  
  22.     }  
  23.       
  24.     public void swap(int[] array, int i, int j) {  
  25.         int temp = array[i];  
  26.         array[i] = array[j];  
  27.         array[j] = temp;          
  28.     }  
  29.       
  30.     public static void main(String[] args) {  
  31.           
  32.         // the size of the array  
  33.         int number = 100000000;  
  34.         // the top k values  
  35.         int k = 100;  
  36.         // the range of the values in the array  
  37.         int range = 1000000001;  
  38.   
  39.         //input for minHeap based method  
  40.         int[] array = new int[number];  
  41.           
  42.         Random random = new Random();  
  43.         for (int i = 0; i < number; i++) {  
  44.             array[i] = random.nextInt(range);  
  45.         }  
  46.           
  47.         TopHundredQuickSort topHundred = new TopHundredQuickSort();  
  48.           
  49.         //start time  
  50.         long t1 = System.currentTimeMillis();   
  51.         topHundred.tophundred(array, 0, array.length - 1, k);  
  52.         //end time  
  53.         long t2 = System.currentTimeMillis();   
  54.           
  55.         System.out.println("The total execution time " +  
  56.                 "of quicksort based method is " + (t2 - t1) +" millisecond!");  
  57.           
  58.         // print out the top k largest values in the top array  
  59.         System.out.println("The top "+ k + "largest values are:");  
  60.         for (int i = 0; i < k; i++) {  
  61.             System.out.println(array[i]);  
  62.         }  
  63.                   
  64.     }  
  65. }  

下面是基于minHeap写的程序。如果你懂heap sort,那么下面的程序很容易理解。

[java]  view plain copy
  1. public class TopHundredHeap {  
  2.       
  3.     public static void main(String[] args) {  
  4.         // the size of the array  
  5.         int number = 100000000;  
  6.         // the top k values  
  7.         int k = 100;  
  8.         // the range of the values in the array  
  9.         int range = 1000000001;  
  10.   
  11.         //input for minHeap based method  
  12.         int[] array = new int[number];  
  13.           
  14.         Random random = new Random();  
  15.         for (int i = 0; i < number; i++) {  
  16.             array[i] = random.nextInt(range);  
  17.         }  
  18.           
  19.         TopHundredHeap thh = new TopHundredHeap();  
  20.           
  21.         long t1, t2;  
  22.         //start time  
  23.         t1 = System.currentTimeMillis();   
  24.         int[] top = thh.topHundred(array, k);  
  25.           
  26.         //end time  
  27.         t2 = System.currentTimeMillis();   
  28.         System.out.println("The total execution time of " +  
  29.                 "quicksort based method is " + (t2 - t1) +" millisecond!");  
  30.           
  31.         // print out the top k largest values in the top array  
  32.         System.out.println("The top "+ k + "largest values are:");  
  33.         for (int i = 0; i < k; i++) {  
  34.             System.out.println(top[i]);  
  35.         }  
  36.     }  
  37.       
  38.     public int[] topHundred(int[] array, int k) {  
  39.         // the heap with size k  
  40.         int[] top = new int[k];  
  41.           
  42.         for (int i = 0; i < k; i++) {  
  43.             top[i] = array[i];  
  44.         }  
  45.           
  46.         buildMinHeap(top);  
  47.           
  48.         for (int i = k; i < array.length; i++) {  
  49.             if (top[0] < array[i]) {  
  50.                 top[0] = array[i];  
  51.                 minHeapify(top, 0, top.length);  
  52.             }  
  53.         }  
  54.           
  55.         return top;  
  56.     }  
  57.       
  58.     // create a min heap  
  59.     public void buildMinHeap(int[] array) {  
  60.         int heapSize = array.length;  
  61.         for (int i = array.length / 2 - 1; i >= 0; i--) {  
  62.             minHeapify(array, i, heapSize);  
  63.         }  
  64.     }  
  65.       
  66.      /// MinHeapify is to build the min heap from the 'position'  
  67.     public void minHeapify(int[] array, int position, int heapSize)  
  68.     {  
  69.         int left = left(position);  
  70.         int right = right(position);  
  71.         int maxPosition = position;  
  72.           
  73.         if (left < heapSize && array[left] < array[position]) {  
  74.             maxPosition = left;  
  75.         }  
  76.           
  77.         if (right < heapSize && array[right] < array[maxPosition]) {  
  78.             maxPosition = right;  
  79.         }  
  80.           
  81.         if (position != maxPosition) {  
  82.             swap(array, position, maxPosition);  
  83.             minHeapify(array, maxPosition, heapSize);  
  84.         }  
  85.     }  
  86.       
  87.     public void swap(int[] array, int i, int j) {  
  88.         int temp = array[i];  
  89.         array[i] = array[j];  
  90.         array[j] = temp;          
  91.     }  
  92.       
  93.     /// return the left child position  
  94.     public int left(int i)  
  95.     {  
  96.         return 2 * i + 1;  
  97.     }  
  98.     /// return the right child position  
  99.     public int right(int i)  
  100.     {  
  101.         return 2 * i + 2;  
  102.     }   
  103. }  

时间复杂度分析:

基于minheap方法 的时间复杂度是 O(lg K * N), 基于quicksort 方法的平均时间复杂度是 O(N),但是最差是O(N^2). 这也是为何基于quicksort 方法它的时间不稳定的原因。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值