java heapsort_Java HeapSort

/**

*

*

*

Copyright 1994-2018 JasonInternational

*

All rights reserved.

*

Created on 2018年4月10日

*

Created by Jason

*

*

*/

package cn.ucaner.algorithm.sorts;

/**

* Heapsort is a comparison-based sorting algorithm to create a sorted array (or

* list), and is part of the selection sort family. Although somewhat slower in

* practice on most machines than a well-implemented quicksort, it has the

* advantage of a more favorable worst-case O(n log n) runtime.

*

* Family: Selection.

* Space: In-place.

* Stable: False.

*

* Average case = O(n*log n)

* Worst case = O(n*log n)

* Best case = O(n*log n)

*

* @see Heap Sort (Wikipedia)

*

* @author Justin Wetherell

*/

public class HeapSort> {

private HeapSort() { }

public static > T[] sort(T[] unsorted) {

createHeap(unsorted);

sortHeap(unsorted);

return unsorted;

}

private static > void sortHeap(T[] unsorted) {

int length = unsorted.length;

for (int index = length - 1; index > 0; index--) {

swap(0, index, unsorted); // swap root with the last heap element

int i = 0; // index of the element being moved down the tree

while (true) {

int left = (i * 2) + 1;

if (left >= index) // node has no left child

break;

int right = left + 1;

if (right >= index) { // node has a left child, but no right child

if (unsorted[left].compareTo(unsorted[i]) > 0)

swap(left, i, unsorted); // if left child is greater than node

break;

}

T ithElement = unsorted[i];

T leftElement = unsorted[left];

T rightElement = unsorted[right];

if (ithElement.compareTo(leftElement) < 0) { // (left > i)

if (unsorted[left].compareTo(rightElement) > 0) { // (left > right)

swap(left, i, unsorted);

i = left;

continue;

}

// (left > i)

swap(right, i, unsorted);

i = right;

continue;

}

// (i > left)

if (rightElement.compareTo(ithElement) > 0) {

swap(right, i, unsorted);

i = right;

continue;

}

// (n > left) & (n > right)

break;

}

}

}

private static > void createHeap(T[] unsorted) {

// Creates a max heap

int size = 0;

int length = unsorted.length;

for (int i = 0; i < length; i++) {

T e = unsorted[i];

size = add(size, e, unsorted);

}

}

private static > int add(int size, T element, T[] unsorted) {

int length = size;

int i = length;

unsorted[length++] = element;

T e = unsorted[i];

int parentIndex = ((i - 1) / 2);

T parent = unsorted[parentIndex];

while (e.compareTo(parent) > 0) {

swap(parentIndex, i, unsorted);

i = parentIndex;

e = unsorted[i];

parentIndex = ((i - 1) / 2);

parent = unsorted[parentIndex];

}

return length;

}

private static > void swap(int parentIndex, int childIndex, T[] unsorted) {

T parent = unsorted[parentIndex];

unsorted[parentIndex] = unsorted[childIndex];

unsorted[childIndex] = parent;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值