Skip to content

Commit ccb76d1

Browse files
williamfisetclaude
andauthored
Refactor Heapsort: add docs, simplify sink with for loop (#1310)
* Refactor Heapsort: add docs, simplify sink with for loop Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Revert sink to while loop, improve comments Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b553466 commit ccb76d1

1 file changed

Lines changed: 24 additions & 25 deletions

File tree

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
/**
2-
* Implementation of heapsort
2+
* In-place heapsort. Builds a max-heap in O(n), then repeatedly extracts the maximum.
3+
*
4+
* <p>Time: O(n log(n))
5+
*
6+
* <p>Space: O(1)
37
*
48
* @author William Fiset, william.alexandre.fiset@gmail.com
59
*/
610
package com.williamfiset.algorithms.sorting;
711

8-
import java.util.*;
12+
import java.util.Arrays;
913

1014
public class Heapsort implements InplaceSort {
1115

@@ -15,39 +19,38 @@ public void sort(int[] values) {
1519
}
1620

1721
private static void heapsort(int[] ar) {
18-
if (ar == null) return;
22+
if (ar == null)
23+
return;
1924
int n = ar.length;
2025

21-
// Heapify, converts array into binary heap O(n), see:
22-
// http://www.cs.umd.edu/~meesh/351/mount/lectures/lect14-heapsort-analysis-part.pdf
23-
for (int i = Math.max(0, (n / 2) - 1); i >= 0; i--) {
26+
// Build max-heap from the array bottom-up in O(n).
27+
for (int i = n / 2 - 1; i >= 0; i--)
2428
sink(ar, n, i);
25-
}
2629

27-
// Sorting bit
30+
// Extract elements one by one: move the root (max element) to the end of the unsorted
31+
// region, shrink the heap by one, and sink the new root to restore the heap property.
2832
for (int i = n - 1; i >= 0; i--) {
2933
swap(ar, 0, i);
3034
sink(ar, i, 0);
3135
}
3236
}
3337

38+
// Sinks element at index i down to its correct position in a max-heap of size n.
3439
private static void sink(int[] ar, int n, int i) {
3540
while (true) {
36-
int left = 2 * i + 1; // Left node
37-
int right = 2 * i + 2; // Right node
41+
int left = 2 * i + 1;
42+
int right = 2 * i + 2;
3843
int largest = i;
3944

40-
// Right child is larger than parent
41-
if (right < n && ar[right] > ar[largest]) largest = right;
45+
if (left < n && ar[left] > ar[largest])
46+
largest = left;
47+
if (right < n && ar[right] > ar[largest])
48+
largest = right;
49+
if (largest == i)
50+
return;
4251

43-
// Left child is larger than parent
44-
if (left < n && ar[left] > ar[largest]) largest = left;
45-
46-
// Move down the tree following the largest node
47-
if (largest != i) {
48-
swap(ar, largest, i);
49-
i = largest;
50-
} else break;
52+
swap(ar, largest, i);
53+
i = largest;
5154
}
5255
}
5356

@@ -57,14 +60,10 @@ private static void swap(int[] ar, int i, int j) {
5760
ar[j] = tmp;
5861
}
5962

60-
/* TESTING */
61-
6263
public static void main(String[] args) {
6364
Heapsort sorter = new Heapsort();
6465
int[] array = {10, 4, 6, 4, 8, -13, 2, 3};
6566
sorter.sort(array);
66-
// Prints:
67-
// [-13, 2, 3, 4, 4, 6, 8, 10]
68-
System.out.println(java.util.Arrays.toString(array));
67+
System.out.println(Arrays.toString(array)); // [-13, 2, 3, 4, 4, 6, 8, 10]
6968
}
7069
}

0 commit comments

Comments
 (0)