|
| 1 | +package com.examplehub.sorts; |
| 2 | + |
| 3 | +import com.examplehub.utils.SortUtils; |
| 4 | + |
| 5 | +public class HeapSort implements Sort{ |
| 6 | + |
| 7 | + @Override |
| 8 | + public void sort(int[] numbers) { |
| 9 | + heapInsert(numbers); |
| 10 | + int length = numbers.length; |
| 11 | + while (length > 1) { |
| 12 | + SortUtils.swap(numbers, 0, --length); |
| 13 | + heapify(numbers, 0, length); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + public void heapInsert(int[] numbers) { |
| 18 | + for (int i = 0; i < numbers.length; ++i) { |
| 19 | + int currentIndex = i; |
| 20 | + int parentIndex = (currentIndex - 1) / 2; |
| 21 | + while (numbers[currentIndex] > numbers[parentIndex]) { |
| 22 | + SortUtils.swap(numbers, currentIndex, parentIndex); |
| 23 | + currentIndex = parentIndex; |
| 24 | + parentIndex = (currentIndex - 1) / 2; |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public void heapify(int[] numbers, int index, int length) { |
| 30 | + int leftIndex = 2 * index + 1; |
| 31 | + int rightIndex = 2 * index + 2; |
| 32 | + while (leftIndex < length) { |
| 33 | + int maxIndex = index; |
| 34 | + if (numbers[leftIndex] > numbers[maxIndex]) { |
| 35 | + maxIndex = leftIndex; |
| 36 | + } |
| 37 | + if (rightIndex < length && numbers[rightIndex] > numbers[maxIndex]) { |
| 38 | + maxIndex = rightIndex; |
| 39 | + } |
| 40 | + if (maxIndex == index) { |
| 41 | + break; |
| 42 | + } |
| 43 | + SortUtils.swap(numbers, maxIndex, index); |
| 44 | + index = maxIndex; |
| 45 | + leftIndex = 2 * index + 1; |
| 46 | + rightIndex = 2 * index + 2; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public <T extends Comparable<T>> void sort(T[] array) { |
| 52 | + heapInsert(array); |
| 53 | + int length = array.length; |
| 54 | + while (length > 1) { |
| 55 | + SortUtils.swap(array, 0, --length); |
| 56 | + heapify(array, 0, length); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public <T extends Comparable<T>>void heapInsert(T[] numbers) { |
| 61 | + for (int i = 0; i < numbers.length; ++i) { |
| 62 | + int currentIndex = i; |
| 63 | + int parentIndex = (currentIndex - 1) / 2; |
| 64 | + while (numbers[currentIndex].compareTo(numbers[parentIndex]) > 0) { |
| 65 | + SortUtils.swap(numbers, currentIndex, parentIndex); |
| 66 | + currentIndex = parentIndex; |
| 67 | + parentIndex = (currentIndex - 1) / 2; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public <T extends Comparable<T>> void heapify(T[] numbers, int index, int length) { |
| 73 | + int leftIndex = 2 * index + 1; |
| 74 | + int rightIndex = 2 * index + 2; |
| 75 | + while (leftIndex < length) { |
| 76 | + int maxIndex = index; |
| 77 | + if (numbers[leftIndex].compareTo(numbers[maxIndex]) > 0) { |
| 78 | + maxIndex = leftIndex; |
| 79 | + } |
| 80 | + if (rightIndex < length && numbers[rightIndex].compareTo(numbers[maxIndex]) > 0) { |
| 81 | + maxIndex = rightIndex; |
| 82 | + } |
| 83 | + if (maxIndex == index) { |
| 84 | + break; |
| 85 | + } |
| 86 | + SortUtils.swap(numbers, maxIndex, index); |
| 87 | + index = maxIndex; |
| 88 | + leftIndex = 2 * index + 1; |
| 89 | + rightIndex = 2 * index + 2; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments