zoukankan      html  css  js  c++  java
  • 七大基本排序算法之堆排序

    package Sort;
    
    import java.io.IOException;
    
    import Input.InputString;
    
    public class HeapSort {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                String str = InputString.getString();
                String[] s = str.split(" ");
                int[] a = new int[s.length+1];
                for(int i = 1; i<=s.length;i++){
                    a[i] = Integer.parseInt(s[i-1]);
                }
                heapSort(a);
                for(int i = 1; i<a.length;i++){
                    System.out.print(a[i]+" ");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        public static void heapAdjust(int[] a, int begin,int end){
            int temp = a[begin];
            for(int i = 2*begin; i <= end; i = 2*i){
                if(i < end && a[i] < a[i+1]){
                    i++;
                }
                if(temp >= a[i]){
                    break;
                }
                a[begin] = a[i];
                begin = i;
            }
            a[begin] = temp;
        }
    
        public static void heapSort(int[] a){
            for(int i = a.length/2; i > 0; i--){
                heapAdjust(a, i, a.length-1);
            }
            for(int i = a.length-1; i > 1; i--){
                int temp = a[1];
                a[1] = a[i];
                a[i] = temp;
                heapAdjust(a, 1, i-1);
            }   
        }
    }
    import java.io.IOException;
    import Input.InputString;
    
    /**
     * 堆排序
     * @author xiaomi
     * 2012.4.1
     */
    public class HeapSort { 
        private static int[] heap;
        public static void main(String[] args) throws IOException{
            String s = InputString.getString();
            String[] str = s.split(" ");
            heap = new int[str.length+1];//从下标1开始算起
            for(int i = 0;i < str.length;i++){
                heap[i+1] = Integer.parseInt(str[i]);
            }
            heapSort();
            for(int i = 1;i < heap.length;i++){
                System.out.print(heap[i]+" ");
            }
        }
    
        public static void heapSort(){
            createHeap();
            for(int i = 1;i < heap.length;i++){
                ajustHeap(i);
            }
        }
    
        //每加入一个节点就要向上筛选一次
        public static void createHeap(){
            for(int i = 2; i < heap.length;i++){
                for(int j = i; j/2>=1;j/=2){
                    if(heap[j]>heap[j/2]){
                        int temp = heap[j];
                        heap[j] = heap[j/2];
                        heap[j/2] = temp;
                    }
                }
            }
        }
    
        //每移走一个节点就要向下调整一次
        public static void ajustHeap(int k){
            int temp = heap[1];
            heap[1] = heap[heap.length-k];
            heap[heap.length-k] = temp;
            int i = 1;
            while((2*i) < heap.length-k){
                if((2*i+1) < heap.length-k){
                    if(heap[i] > heap[2*i] && heap[i] > heap[2*i+1]){
                        break;
                    }
                    else{//与较大的一个子节点交换
                        if(heap[2*i] < heap[2*i+1]){
                            int a = heap[i];
                            heap[i] = heap[2*i+1];
                            heap[2*i+1] = a;
                            i = 2*i+1;
                        }else{
                            int a = heap[i];
                            heap[i] = heap[2*i];
                            heap[2*i] = a;
                            i = 2*i;
                        }
                    }
                }else{//不存在右节点的时候
                    if(heap[i] > heap[2*i]){
                        break;
                    }
                    else{
                        int a = heap[i];
                        heap[i] = heap[2*i];
                        heap[2*i] = a;
                        i = 2*i;
                    }
                }   
            }
        }
    }

       

  • 相关阅读:
    单例模式
    Curator Zookeeper分布式锁
    LruCache算法原理及实现
    lombok 简化java代码注解
    Oracle客户端工具出现“Cannot access NLS data files or invalid environment specified”错误的解决办法
    解决mysql Table ‘xxx’ is marked as crashed and should be repaired的问题。
    Redis 3.0 Cluster集群配置
    分布式锁的三种实现方式
    maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令
    How to Use Convolutional Neural Networks for Time Series Classification
  • 原文地址:https://www.cnblogs.com/williamxiao/p/3499943.html
Copyright © 2011-2022 走看看