Skip to content

Commit 7e73135

Browse files
goodnicvarunu28
authored andcommitted
Heap Sort
* Making use of the Kotlin programming language There is no need to explicitly create a temporary variable to swap two array elements. Using the .also function of Kotlin the very same thing can be achieved. * Adding Heap Sort including basic unit tests
1 parent 9eb092d commit 7e73135

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

src/main/kotlin/sort/BubbleSort.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fun <T: Comparable<T>> bubbleSort(array: Array<T>) {
3636
* Swaps the element at two indexes
3737
**/
3838
fun <T: Comparable<T>> swapElements(array: Array<T>, idx1: Int, idx2: Int) {
39-
val temp = array[idx1]
40-
array[idx1] = array[idx2]
41-
array[idx2] = temp
39+
array[idx1] = array[idx2].also {
40+
array[idx2] = array[idx1]
41+
}
4242
}

src/main/kotlin/sort/HeapSort.kt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package sort
2+
3+
/**
4+
* This function implements the Heap Sort.
5+
*
6+
* @param array The array to be sorted
7+
* Sorts the array in increasing order
8+
*
9+
* Worst-case performance O(n*log(n))
10+
* Best-case performance O(n*log(n))
11+
* Average-case performance O(n*log(n))
12+
* Worst-case space complexity O(1) (auxiliary)
13+
*/
14+
fun <T: Comparable<T>> heapSort(array: Array<T>) {
15+
buildMaxHeap(array)
16+
transformMaxHeapToSortedArray(array)
17+
}
18+
19+
/**
20+
* This function changes the element order of the array to represent a max
21+
* binary tree.
22+
*
23+
* @param array The array containing the elements
24+
* @param index Index of the currently largest element
25+
*/
26+
fun <T: Comparable<T>> maxheapify(array: Array<T>, heapSize: Int, index: Int) {
27+
val left = 2 * index + 1
28+
val right = 2 * index + 2
29+
var largest = index
30+
31+
if(left < heapSize && array[left] > array[largest])
32+
largest = left
33+
if(right < heapSize && array[right] > array[largest])
34+
largest = right
35+
if(largest != index) {
36+
swapElements(array, index, largest)
37+
maxheapify(array, heapSize, largest)
38+
}
39+
}
40+
41+
/**
42+
* Arrange the elements of the array to represent a max heap.
43+
*
44+
* @param array The array containing the elements
45+
*/
46+
private fun <T: Comparable<T>> buildMaxHeap(array: Array<T>) {
47+
val n = array.size
48+
for(i in (n / 2 - 1) downTo 0)
49+
maxheapify(array, n, i)
50+
}
51+
52+
/**
53+
* Arrange the elements of the array, which should be in order to represent a
54+
* max heap, into ascending order.
55+
*
56+
* @param array The array containing the elements (max heap representation)
57+
*/
58+
private fun <T: Comparable<T>> transformMaxHeapToSortedArray(array: Array<T>) {
59+
for(i in (array.size - 1) downTo 0) {
60+
swapElements(array, i, 0)
61+
maxheapify(array, i, 0)
62+
}
63+
}
64+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package sort
2+
3+
import org.junit.Assert.assertArrayEquals
4+
import org.junit.Test
5+
6+
class HeapSortTest {
7+
8+
@Test
9+
fun `sort array of integers`() {
10+
val array = arrayOf(4, 3, 2, 8, 1)
11+
heapSort(array)
12+
assertArrayEquals(array, arrayOf(1, 2, 3, 4, 8))
13+
}
14+
15+
@Test
16+
fun `sort sorted array of integers`() {
17+
val array = arrayOf(1, 2, 3, 4, 8)
18+
heapSort(array)
19+
assertArrayEquals(array, arrayOf(1, 2, 3, 4, 8))
20+
}
21+
22+
@Test
23+
fun `sort array of characters`() {
24+
val array = arrayOf('A', 'D', 'E', 'C', 'B')
25+
heapSort(array)
26+
assertArrayEquals(array, arrayOf('A', 'B', 'C', 'D', 'E'))
27+
}
28+
29+
}

0 commit comments

Comments
 (0)