forked from Pranav-173/DSA
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeapsort.java
More file actions
68 lines (68 loc) · 1.9 KB
/
Heapsort.java
File metadata and controls
68 lines (68 loc) · 1.9 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.Scanner;
import java.util.Arrays;
public class Heapsort {
/*
* Edge-case behavior:
* - Negative size is rejected.
* - Size 0 exits early with "Nothing to sort/search".
* - Null/empty arrays are ignored by sort/heapify.
*/
public void sort(int arr[]) {
if (arr == null || arr.length == 0) {
return;
}
int n = arr.length;
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i >= 0; i--) {
int tmp = arr[0];
arr[0] = arr[i];
arr[i] = tmp;
heapify(arr, i, 0);
}
}
void heapify(int arr[], int n, int i) {
if (arr == null || n <= 0) {
return;
}
int max = i;
int leftChild = 2 * i + 1;
int rightChild = 2 * i + 2;
if (leftChild < n && arr[leftChild] > arr[max])
max = leftChild;
if (rightChild < n && arr[rightChild] > arr[max])
max = rightChild;
if (max != i) {
int swap = arr[i];
arr[i] = arr[max];
arr[max] = swap;
heapify(arr, n, max);
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in the array : ");
int n = sc.nextInt();
if (n < 0) {
System.out.println("Invalid input: size cannot be negative.");
sc.close();
return;
}
if (n == 0) {
System.out.println("Nothing to sort/search");
sc.close();
return;
}
System.out.println("Enter array elements : ");
int[] arr = new int[n];
for(int i = 0; i <n; i++){
arr[i] = sc.nextInt();
}
System.out.println("Original array : "+ Arrays.toString(arr));
Heapsort hs = new Heapsort();
hs.sort(arr);
System.out.println("Sorted array:"+ Arrays.toString(arr));
sc.close();
}
}