Skip to content

Commit ffd6ab4

Browse files
heap sort (examplehub#190)
* heap sort * fixed heap sort
1 parent d04f4fc commit ffd6ab4

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.examplehub.sorts;
2+
3+
import com.examplehub.domain.Student;
4+
import com.examplehub.utils.RandomUtils;
5+
import com.examplehub.utils.SortUtils;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
13+
class HeapSortTest {
14+
15+
private Sort sort;
16+
17+
@BeforeEach
18+
public void before() {
19+
sort = new HeapSort();
20+
}
21+
22+
@Test
23+
void testSort() {
24+
int[] ints = RandomUtils.randomInts(-50, 50, 100);
25+
sort.sort(ints);
26+
assertTrue(SortUtils.isSorted(ints));
27+
}
28+
29+
@Test
30+
void testSortIntegers() {
31+
Integer[] integers =
32+
Arrays.stream(RandomUtils.randomInts(-50, 50, 100)).boxed().toArray(Integer[]::new);
33+
sort.sort(integers);
34+
assertTrue(SortUtils.isSorted(integers));
35+
}
36+
37+
@Test
38+
void testSortedDoubles() {
39+
Double[] doubles = new Double[100];
40+
double[] tempDoubles = RandomUtils.randomDoubles(-50, 50, 100);
41+
for (int i = 0; i < doubles.length; ++i) {
42+
doubles[i] = tempDoubles[i];
43+
}
44+
sort.sort(doubles);
45+
assertTrue(SortUtils.isSorted(doubles));
46+
}
47+
48+
@Test
49+
void testComparable() {
50+
Student[] students = new Student[5];
51+
students[0] = new Student(1, 98, 99, 100);
52+
students[1] = new Student(2, 100, 99, 98);
53+
students[2] = new Student(3, 100, 98, 99);
54+
students[3] = new Student(4, 100, 100, 100);
55+
students[4] = new Student(5, 99, 99, 99);
56+
sort.sort(students);
57+
assertTrue(SortUtils.isSorted(students));
58+
}
59+
}

0 commit comments

Comments
 (0)