/*@(#)ArrayUtils.java 2017-4-22
* Copy Right 2017 Bank of Communications Co.Ltd.
* All Copyright Reserved
*/
package com.sort.cn;
/**
* TODO Document ArrayUtils
* <p>
* @version 1.0.0,2017-4-22
* @author Singit
* @since 1.0.0
*/
public class ArrayUtils {
public static void printArray(int[] array) {
System.out.print("{");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1) {
System.out.print(", ");
}
}
System.out.println("}");
}
public static void exchangeElements(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}
/*@(#)HeapSort.java 2017-4-22
* Copy Right 2017 Bank of Communications Co.Ltd.
* All Copyright Reserved
*/
package com.sort.cn;
/**
* TODO Document HeapSort
* <p>
* @version 1.0.0,2017-4-22
* @author Singit
* @since 1.0.0
*/
public class HeapSort {
public static void main(String[] args) {
int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3 };
System.out.println("Before heap:");
ArrayUtils.printArray(array);
heapSort(array);
System.out.println("After heap sort:");
ArrayUtils.printArray(array);
}
public static void heapSort(int[] array) {
if (array == null || array.length <= 1) {
return;
}
buildMaxHeap(array);
for (int i = array.length - 1; i >= 1; i--) {
ArrayUtils.exchangeElements(array, 0, i);
maxHeap(array, i, 0);
}
}
private static void buildMaxHeap(int[] array) {
if (array == null || array.length <= 1) {
return;
}
int half = array.length / 2;
for (int i = half; i >= 0; i--) {
maxHeap(array, array.length, i);
}
}
private static void maxHeap(int[] array, int heapSize, int index) {
int left = index * 2 + 1;
int right = index * 2 + 2;
int largest = index;
if (left < heapSize && array[left] > array[index]) {
largest = left;
}
if (right < heapSize && array[right] > array[largest]) {
largest = right;
}
if (index != largest) {
ArrayUtils.exchangeElements(array, index, largest);
maxHeap(array, heapSize, largest);
}
}
}
输出结果:
Before heap:
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3}
After heap sort:
{-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}