forked from sambit77/Algoexpert-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
64 lines (59 loc) · 1.25 KB
/
HeapSort.java
File metadata and controls
64 lines (59 loc) · 1.25 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
//Time Complexity O(n log n) (because for n numbers at max we have to shiftDown log n)
//Space Complexity O(1)
import java.util.*;
class A
{
public static void main(String[] args)
{
int[] arr = new int[]{8,5,2,9,5,6,3};
int[] sorted = heapSort(arr);
System.out.println("Sorted Array is "+Arrays.toString(sorted));
}
public static int[] heapSort(int[] arr)
{
buildMaxHeap(arr);
for(int i = arr.length-1; i >= 0 ; i--)
{
swap(0,i,arr);
shiftDown(0,i-1,arr);
}
return arr;
}
public static void buildMaxHeap(int[] arr)
{
int firstNonLeaf = arr.length/2;
for(int i = firstNonLeaf+1; i >=0 ; i--)
{
shiftDown(i,arr.length-1,arr);
}
}
public static void shiftDown(int current,int size,int[] arr)
{
int left = ((2*current)+1)/2;
int right = ((2*current)+2)/2;
int largest = 0;
if(left <= size && arr[left] > arr[current])
{
largest=left;
}
else
{
largest=current;
}
if(right<=size && arr[right] > arr[largest])
{
largest=right;
}
if(largest != current)
{
swap(largest,current,arr);
shiftDown(current,size,arr);
}
}
public static void swap(int i, int j ,int[] arr)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}