Skip to content

Commit 285219c

Browse files
fanatixanmaibin
authored andcommitted
Bael-2210 - Heap Sort (#5446)
* implementing heap * Heap sort refactor
1 parent 23b1323 commit 285219c

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.baeldung.heapsort;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class Heap<E extends Comparable<E>> {
8+
9+
private List<E> elements = new ArrayList<>();
10+
11+
public static <E extends Comparable<E>> List<E> sort(Iterable<E> elements) {
12+
Heap<E> heap = of(elements);
13+
14+
List<E> result = new ArrayList<>();
15+
16+
while (!heap.isEmpty()) {
17+
result.add(heap.pop());
18+
}
19+
20+
return result;
21+
}
22+
23+
public static <E extends Comparable<E>> Heap<E> of(E... elements) {
24+
return of(Arrays.asList(elements));
25+
}
26+
27+
public static <E extends Comparable<E>> Heap<E> of(Iterable<E> elements) {
28+
Heap<E> result = new Heap<>();
29+
for (E element : elements) {
30+
result.add(element);
31+
}
32+
return result;
33+
}
34+
35+
public void add(E e) {
36+
elements.add(e);
37+
int elementIndex = elements.size() - 1;
38+
while (!isRoot(elementIndex) && !isCorrectChild(elementIndex)) {
39+
int parentIndex = parentIndex(elementIndex);
40+
swap(elementIndex, parentIndex);
41+
elementIndex = parentIndex;
42+
}
43+
}
44+
45+
public E pop() {
46+
if (isEmpty()) {
47+
throw new IllegalStateException("You cannot pop from an empty heap");
48+
}
49+
50+
E result = elementAt(0);
51+
52+
int lasElementIndex = elements.size() - 1;
53+
swap(0, lasElementIndex);
54+
elements.remove(lasElementIndex);
55+
56+
int elementIndex = 0;
57+
while (!isLeaf(elementIndex) && !isCorrectParent(elementIndex)) {
58+
int smallerChildIndex = smallerChildIndex(elementIndex);
59+
swap(elementIndex, smallerChildIndex);
60+
elementIndex = smallerChildIndex;
61+
}
62+
63+
return result;
64+
}
65+
66+
public boolean isEmpty() {
67+
return elements.isEmpty();
68+
}
69+
70+
private boolean isRoot(int index) {
71+
return index == 0;
72+
}
73+
74+
private int smallerChildIndex(int index) {
75+
int leftChildIndex = leftChildIndex(index);
76+
int rightChildIndex = rightChildIndex(index);
77+
78+
if (!isValidIndex(rightChildIndex)) {
79+
return leftChildIndex;
80+
}
81+
82+
if (elementAt(leftChildIndex).compareTo(elementAt(rightChildIndex)) < 0) {
83+
return leftChildIndex;
84+
}
85+
86+
return rightChildIndex;
87+
}
88+
89+
private boolean isLeaf(int index) {
90+
return !isValidIndex(leftChildIndex(index));
91+
}
92+
93+
private boolean isCorrectParent(int index) {
94+
return isCorrect(index, leftChildIndex(index)) && isCorrect(index, rightChildIndex(index));
95+
}
96+
97+
private boolean isCorrectChild(int index) {
98+
return isCorrect(parentIndex(index), index);
99+
}
100+
101+
private boolean isCorrect(int parentIndex, int childIndex) {
102+
if (!isValidIndex(parentIndex) || !isValidIndex(childIndex)) {
103+
return true;
104+
}
105+
106+
return elementAt(parentIndex).compareTo(elementAt(childIndex)) < 0;
107+
}
108+
109+
private boolean isValidIndex(int index) {
110+
return index < elements.size();
111+
}
112+
113+
private void swap(int index1, int index2) {
114+
E element1 = elementAt(index1);
115+
E element2 = elementAt(index2);
116+
elements.set(index1, element2);
117+
elements.set(index2, element1);
118+
}
119+
120+
private E elementAt(int index) {
121+
return elements.get(index);
122+
}
123+
124+
private int parentIndex(int index) {
125+
return (index - 1) / 2;
126+
}
127+
128+
private int leftChildIndex(int index) {
129+
return 2 * index + 1;
130+
}
131+
132+
private int rightChildIndex(int index) {
133+
return 2 * index + 2;
134+
}
135+
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.heapsort;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import org.junit.Test;
9+
10+
public class HeapUnitTest {
11+
12+
@Test
13+
public void givenNotEmptyHeap_whenPopCalled_thenItShouldReturnSmallestElement() {
14+
// given
15+
Heap<Integer> heap = Heap.of(3, 5, 1, 4, 2);
16+
17+
// when
18+
int head = heap.pop();
19+
20+
// then
21+
assertThat(head).isEqualTo(1);
22+
}
23+
24+
@Test
25+
public void givenNotEmptyIterable_whenSortCalled_thenItShouldReturnElementsInSortedList() {
26+
// given
27+
List<Integer> elements = Arrays.asList(3, 5, 1, 4, 2);
28+
29+
// when
30+
List<Integer> sortedElements = Heap.sort(elements);
31+
32+
// then
33+
assertThat(sortedElements).isEqualTo(Arrays.asList(1, 2, 3, 4, 5));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)