• 堆排序TopK


    package test;
    
    import java.util.Random;
    
    public class TSort {
    
        public static void main(String[] args) {
            TSort tsort = new TSort();
            tsort.test();
        }
    
        public void test() {
            TopHundredHeap.test(null);
        }
    
        public void testt(String a, double[] d, String[] t, String... c) {
            Math.random();
        }
    }
    
    class TopHundredHeap {
    
        public static void test(String[] args) {
            // the size of the array
            int number = 100000000;
            // the top k values
            int k = 100;
            // the range of the values in the array
            int range = 1000000001;
    
            // input for minHeap based method
            int[] array = new int[number];
    
            Random random = new Random();
            for (int i = 0; i < number; i++) {
                array[i] = random.nextInt(range);
            }
    
            TopHundredHeap thh = new TopHundredHeap();
    
            long t1, t2;
            // start time
            t1 = System.currentTimeMillis();
            int[] top = thh.topHundred(array, k);
    
            // end time
            t2 = System.currentTimeMillis();
            System.out.println("The total execution time of " + "quicksort based method is " + (t2 - t1) + " millisecond!");
    
            // print out the top k largest values in the top array
            System.out.println("The top " + k + "largest values are:");
            for (int i = 0; i < k; i++) {
                System.out.println(top[i]);
            }
        }
    
        public int[] topHundred(int[] array, int k) {
            // the heap with size k
            int[] top = new int[k];
    
            for (int i = 0; i < k; i++) {
                top[i] = array[i];
            }
    
            buildMinHeap(top);
    
            for (int i = k; i < array.length; i++) {
                if (top[0] < array[i]) {
                    top[0] = array[i];
                    minHeapify(top, 0, top.length);
                }
            }
    
            return top;
        }
    
        // create a min heap
        public void buildMinHeap(int[] array) {
            int heapSize = array.length;
            for (int i = array.length / 2 - 1; i >= 0; i--) {
                minHeapify(array, i, heapSize);
            }
        }
    
        /// MinHeapify is to build the min heap from the 'position'
        public void minHeapify(int[] array, int position, int heapSize) {
            int left = left(position);
            int right = right(position);
            int maxPosition = position;
    
            if (left < heapSize && array[left] < array[position]) {
                maxPosition = left;
            }
    
            if (right < heapSize && array[right] < array[maxPosition]) {
                maxPosition = right;
            }
    
            if (position != maxPosition) {
                swap(array, position, maxPosition);
                minHeapify(array, maxPosition, heapSize);
            }
        }
    
        public void swap(int[] array, int i, int j) {
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    
        /// return the left child position
        public int left(int i) {
            return 2 * i + 1;
        }
    
        /// return the right child position
        public int right(int i) {
            return 2 * i + 2;
        }
    }
  • 相关阅读:
    更改数据库sa密码
    单例模式实现 (Singleton)
    linux特殊符号大全
    “增强现实与多媒体”博客园开张了
    关于.NET运行多个版本
    DevExpress V7.2.3源码 Salesman or Opium Up to you
    there is no bool datatype in oralce datatype system
    .NET时代,还有多少人读库源码 求FCL源码
    当学术界在解决其理论问题,工程界形成形成了一个已开发者为核心的生态链
    Div css 管理后台 简易Demo
  • 原文地址:https://www.cnblogs.com/jpit/p/7375614.html
Copyright © 2020-2023  润新知