-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
34 lines (29 loc) · 854 Bytes
/
HeapSort.java
File metadata and controls
34 lines (29 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package Sorting;
public class HeapSort {
public static void heapSort(int[] A) {
MaxHeap h = new MaxHeap(A);
h.buildMaxHeap();
for (int i = h.arraySize - 1; i > 0; i--) {
swap(h.array, 0, i);
h.heapSize--;
h.maxHeapify(0);
}
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String[] args) {
int[] numbers = {3, 1, 4, 9, -2, 5, 10, 2};
for (int n : numbers) {
System.out.print(n + " ");
}
heapSort(numbers);
System.out.println();
for (int n : numbers) {
System.out.print(n + " ");
}
// System.out.println("\nMAX " + Integer.MAX_VALUE);
}
}