-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeapSort.java
More file actions
77 lines (63 loc) · 2.37 KB
/
Copy pathHeapSort.java
File metadata and controls
77 lines (63 loc) · 2.37 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
69
70
71
72
73
74
75
76
77
import java.util.Scanner; //To get command line input from users
public class HeapSort {
public void sort(int arr[]) //Function where sorting occurs
{
int n = arr.length; //n denotes the length of the array
for (int i = n/2-1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0); //To make the array into max heap
}
}
void heapify(int arr[], int n, int i) //Function for creating max heap
{
int largest = i;
int l = 2 * i + 1; //Left child
int r = 2 * i + 2; //Right child
if (l < n && arr[l] > arr[largest]) //If left child is greater than parent then set largest is left child
largest = l;
if (r < n && arr[r] > arr[largest]) //If right child is greater than parent then set largest is right child
largest = r;
if (largest != i) { //After finding largest between left and right child swap largest with the parent node
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
static void printArray(int arr[]) //Function for printing the array
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
/* if you want to give manual enteries the uncomment this
System.out.print("Enter no of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.print("Enter the elements: ");
for(int i =0;i<n;i++)
{
int temp = sc.nextInt();
arr[i] = temp;
}
*/
int n = args.length;
int[] arr = new int[n];
for(int i=0;i<n;i++)
{
arr[i] = Integer.parseInt(args[i]);
}
HeapSort ob = new HeapSort();
ob.sort(arr);
System.out.println("Sorted array is");
printArray(arr);
}
}