java实现的堆排序(java)

来源:互联网 发布:淘宝导航条怎么去掉 编辑:程序博客网 时间:2024/05/29 18:11
//堆排序public class HeapSorter {// 主线程main方法public static void main(String[] args) {Integer[] testArr = new Integer[] { 7, 9, 1, 6 };heapSort(testArr, new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o1 - o2;}});printArr(testArr);}// 对数组进行堆排序public static final <T> void heapSort(T[] testArr, Comparator<T> comparator) {// 将数组初始化为最大堆buildHeap(testArr, comparator);// 循环排序,依此将最大的数归位.for (int i = testArr.length - 1; i > 0; i--) {// 交换最后一个元素与根元素(归位最大数)swap(testArr, i, 0);// 维护堆排序heapify(testArr, 0, i + 1, comparator);}}// 打印数组元素public static final <T> void printArr(T[] testArr) {for (T t : testArr) {System.out.print(t.toString() + "; ");}System.out.println();}// 交换数组中的2个元素public static final <T> void swap(T[] testArr, int i, int j) {T tempEle = testArr[i];testArr[i] = testArr[j];testArr[j] = tempEle;}// 把数组转化为最大堆(最大堆的定义是用递归定义的,这里构造的时候,就采用递归思想)public static final <T> void buildHeap(T[] testArr, Comparator<T> comparator) {                // 从最后一个节点的父节点开始. 以它为根节点的树具有堆树性质, 然后一层一层的向上后退for (int i = (testArr.length / 2) - 1; i >= 0; i--) {heapify(testArr, i, comparator);}}// 维护以startIndex为根元素的堆树, 前提是startIndex为根的左右子树具有堆的性质public static final <T> void heapify(T[] testArr, int startIndex, int size, Comparator<T> comparator) {// 取得最大值int nextIndex = startIndex;int leftIndex = left(startIndex);int rightIndex = right(startIndex);if (leftIndex < size - 1 && comparator.compare(testArr[leftIndex], testArr[startIndex]) > 0)nextIndex = leftIndex;if (rightIndex < size - 1 && comparator.compare(testArr[rightIndex], testArr[nextIndex]) > 0)nextIndex = rightIndex;if (nextIndex == startIndex)return;swap(testArr, startIndex, nextIndex);// 维护最大堆heapify(testArr, nextIndex, size, comparator);}// 维护以startIndex为根元素的堆树public static final <T> void heapify(T[] testArr, int startIndex, Comparator<T> comparator) {heapify(testArr, startIndex, testArr.length, comparator);}// 获得以startIndex为根元素的左节点, 返回的是数组索引public static final int left(int startIndex) {return ((startIndex + 1) << 1) - 1;// 注意这里位运算的优先级问题}// 获得以startIndex为根元素的右节点, 返回的是数组索引public static final int right(int startIndex) {return ((startIndex + 1) << 1);}}
0 0