zoukankan      html  css  js  c++  java
  • 堆排序(利用最大堆)

    1. package heap;   
    2.   
    3. import java.math.BigInteger;   
    4.   
    5. /**
    6. * 最大堆最小堆性质:
    7. * 完全二叉树
    8. * left=2i;
    9. * right=2i+1;
    10. * 最大堆:除根节点外,子节点<父节点
    11. * 最小堆:除根节点外,子节点>父节点
    12. * 堆排序算法复杂度:o(n*lgn)
    13. *
    14. * @author B.Chen
    15. *
    16. */  
    17. public class MaxHeap {   
    18.   
    19.     public int heapSize;   
    20.   
    21.     public int parent(int i) {   
    22.         return i / 2;   
    23.      }   
    24.   
    25.     public int left(int i) {   
    26.         return 2 * i;   
    27.      }   
    28.   
    29.     public int right(int i) {   
    30.         return 2 * i + 1;   
    31.      }   
    32.   
    33.     public void maxHeapify(int[] a, int i) {   
    34.         int l = left(i);   
    35.         int r = right(i);   
    36.         int largest = i;   
    37.         if (l < heapSize) {   
    38.             if (a[l] > a[i]) {   
    39.                  largest = l;   
    40.              }   
    41.          }   
    42.         if (r < heapSize) {   
    43.             if (a[r] > a[largest]) {   
    44.                  largest = r;   
    45.              }   
    46.          }   
    47.         if (largest != i) {   
    48.             int temp = a[i];   
    49.              a[i] = a[largest];   
    50.              a[largest] = temp;   
    51.              maxHeapify(a, largest);   
    52.          }   
    53.      }   
    54.   
    55.     public void builtMaxHeap(int[] a) {   
    56.          heapSize = a.length;   
    57.         for (int i = (a.length - 1) / 2; i >= 0; i--) {   
    58.              maxHeapify(a, i);   
    59.          }   
    60.      }   
    61.   
    62.     public void heapSort(int[] a) {   
    63.          builtMaxHeap(a);   
    64.         for (int i = a.length - 1; i > 0; i--) {   
    65.             int temp = a[0];   
    66.              a[0] = a[i];   
    67.              a[i] = temp;   
    68.              heapSize = heapSize - 1;   
    69.              maxHeapify(a, 0);   
    70.          }   
    71.      }   
    72.   
    73.     public static void main(String[] args) {   
    74.          MaxHeap mh = new MaxHeap();   
    75.         int[] a = new int[] { 7, 6, 4, 2, 8, 3, 1, 5, 9, 0 };   
    76.          mh.heapSort(a);   
    77.         for (int i = 0; i < a.length; i++) {   
    78.              System.out.print(a[i] + " ");   
    79.          }   
    80.      }   
    81.   
    82. }  
    83. 其中2×i可以用二进制表示成i<<1
      2×i+1可以表示成(i<<1)+1
  • 相关阅读:
    OK335x mksd.sh hacking
    Qt jsoncpp 对象拷贝、删除、函数调用 demo
    OK335xS 256M 512M nand flash make ubifs hacking
    Qt QScrollArea and layout in code
    JsonCpp Documentation
    Qt 4.8.5 jsoncpp lib
    Oracle数据库生成UUID
    freemarker得到数组的长度
    FreeMarker中if标签内的判断条件
    freemarker语法
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100618.html
Copyright © 2011-2022 走看看