堆排序(Heapsort)是一种利用数据结构中的堆进行排序的算法,分为构建初始堆,减小堆的元素个数,调整堆共3步。
(一)算法实现
1 protected void sort(int[] toSort) { 2 buildHeap(toSort); 3 for (int i = toSort.length - 1; i > 0; i--) { 4 CommonUtils.swap(toSort, 0, i); 5 adjustHeap(toSort, 0, i); 6 } 7 } 8 9 /** 10 * 11 * @param toSort 12 * array of heap, begins from 0 13 * @param index 14 * index to adjust 15 * @param size 16 * size of heap 17 */ 18 private void adjustHeap(int[] toSort, int index, int size) { 19 int leftNode = index * 2 + 1; 20 int rightNode = leftNode + 1; 21 22 int maxIndex = index; 23 if (leftNode < size && toSort[leftNode] > toSort[maxIndex]) { 24 maxIndex = leftNode; 25 } 26 if (rightNode < size && toSort[rightNode] > toSort[maxIndex]) { 27 maxIndex = rightNode; 28 } 29 if (maxIndex != index) { 30 CommonUtils.swap(toSort, index, maxIndex); 31 adjustHeap(toSort, maxIndex, size); 32 } 33 34 } 35 36 /** 37 * 38 * @param toSort 39 * array to sort 40 */ 41 private void buildHeap(int[] toSort) { 42 int lastNonLeaf = toSort.length / 2 - 1; 43 for (int i = lastNonLeaf; i >= 0; i--) { 44 adjustHeap(toSort, i, toSort.length); 45 } 46 }
1)堆排序是原地排序
2)堆排序的时间复杂度是O(nlgn)
3)堆排序属于比较排序
4)堆排序不是稳定排序算法
(二)仿真结果
**************************************************
Number to Sort is:2500
Array to sort is:{419836,72576,347420,355422,378503,65556,443634,137868,266344,918856...}
Cost time of 【HeapSort】 is(milliseconds):1
Sort result of 【HeapSort】:{185,874,996,1232,1448,2357,2728,2854,3137,3291...}
**************************************************
Number to Sort is:25000
Array to sort is:{169570,655593,54301,59080,890711,224726,720131,590749,600165,681962...}
Cost time of 【HeapSort】 is(milliseconds):7
Sort result of 【HeapSort】:{9,107,119,192,297,321,338,359,359,362...}
**************************************************
Number to Sort is:250000
Array to sort is:{233097,327821,972339,26697,803510,598167,178244,117664,904299,195258...}
Cost time of 【HeapSort】 is(milliseconds):59
Sort result of 【HeapSort】:{0,1,3,8,16,24,32,37,45,52...}
相关代码:
1 package com.cnblogs.riyueshiwang.sort; 2 3 import java.util.Arrays; 4 5 public class HeapSort extends abstractSort { 6 @Override 7 protected void sort(int[] toSort) { 8 buildHeap(toSort); 9 for (int i = toSort.length - 1; i > 0; i--) { 10 CommonUtils.swap(toSort, 0, i); 11 adjustHeap(toSort, 0, i); 12 } 13 } 14 15 /** 16 * 17 * @param toSort 18 * array of heap, begins from 0 19 * @param index 20 * index to adjust 21 * @param size 22 * size of heap 23 */ 24 private void adjustHeap(int[] toSort, int index, int size) { 25 int leftNode = index * 2 + 1; 26 int rightNode = leftNode + 1; 27 28 int maxIndex = index; 29 if (leftNode < size && toSort[leftNode] > toSort[maxIndex]) { 30 maxIndex = leftNode; 31 } 32 if (rightNode < size && toSort[rightNode] > toSort[maxIndex]) { 33 maxIndex = rightNode; 34 } 35 if (maxIndex != index) { 36 CommonUtils.swap(toSort, index, maxIndex); 37 adjustHeap(toSort, maxIndex, size); 38 } 39 40 } 41 42 /** 43 * 44 * @param toSort 45 * array to sort 46 */ 47 private void buildHeap(int[] toSort) { 48 int lastNonLeaf = toSort.length / 2 - 1; 49 for (int i = lastNonLeaf; i >= 0; i--) { 50 adjustHeap(toSort, i, toSort.length); 51 } 52 } 53 54 public static void main(String[] args) { 55 for (int j = 0, n = 2500; j < 3; j++, n = n * 10) { 56 System.out 57 .println("**************************************************"); 58 System.out.println("Number to Sort is:" + n); 59 int[] array = CommonUtils.getRandomIntArray(n, 1000000); 60 System.out.print("Array to sort is:"); 61 CommonUtils.printIntArray(array); 62 63 int[] array1 = Arrays.copyOf(array, n); 64 new HeapSort().sortAndprint(array1); 65 } 66 } 67 }