forked from shaik-zaheeruddin/sorting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSortAlgo.java
More file actions
44 lines (40 loc) · 1.23 KB
/
HeapSortAlgo.java
File metadata and controls
44 lines (40 loc) · 1.23 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
package com.sorting;
import java.util.Arrays;
/*
Time And Space for heap
Worst Case Time Complexity is O(n*2)
and best Case Tc O(n*2)
and space is O(1) as there is no use of array is done
*/
public class HeapSortAlgo {
public static void main(String[] args) {
int[] arr = {7,10,3,2,9};
sort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
static void swap(int arr[],int first,int next){
int temp = arr[first];
arr[first] = arr[next];
arr[next] = temp;
}
static int pivot(int[] arr,int start ,int end){
int swapIdx = start;
int pivot = arr[start];
for(int i =start+1;i<=end;i++){
if(pivot>arr[i]){
swapIdx++;
swap(arr,i,swapIdx);
}
}
swap(arr,swapIdx,start);
return swapIdx;
}
static void sort(int[] arr,int start,int end){
if(start<end){
int pivotIdx = pivot(arr,start,end);
//now array 2,3,7,10,9
sort(arr,start,pivotIdx-1);//for the left side of the pivotIndex
sort(arr,pivotIdx+1,end);//for the right side of the pivotIndex
}
}
}