-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhpsort.java
More file actions
64 lines (61 loc) · 1.46 KB
/
Copy pathhpsort.java
File metadata and controls
64 lines (61 loc) · 1.46 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
public static int[] heapSort(int[] nums) {
//build the heap
for(int i=nums.length-1;i>=0;i--)
//merge the 2 sub-heaps below i, by bubbling nums[i] down to the correct location
sink(nums,i,nums.length);
//turn it into a sorted list
for(int i=nums.length-1;i>0;i--){
//remove the root, and put the last leaf at the root of the heap
int tmp = nums[i];
nums[i]=nums[0];
nums[0] = tmp;
//bubble the new root down to the correct location
sink(nums,0,i);
}
return nums;
}
static void sink(int[] nums, int start, int end){
int val = nums[start];
int i = start, next = 2*i+1;
while(next<end){
if(next+1<end && nums[next+1]>nums[next])
next++;
if(nums[next]<=val)
break;
nums[i]=nums[next];
i=next;
next = 2*i+1; //2*i+1, 2*i+2 are the children of i
}
nums[i]=val;
}
Original code: O(nlogn) build heap
public static int[] heapSort(int[] nums) {
//build the heap
for(int i=1;i<nums.length;i++){
int val = nums[i];
int j=i, next = (j-1)/2; //(j-i)/2 is the index of this location's parent
while(j>0 && nums[next]<val){
nums[j]=nums[next];
j=next;
next = (j-1)/2;
}
nums[j]=val;
}
//turn it into a sorted list
for(int i=nums.length-1;i>0;i--){
int val = nums[i];
nums[i]=nums[0];
int j = 0, next = 1;
while(next<i){
if(next+1<i && nums[next+1]>nums[next])
next++;
if(nums[next]<=val)
break;
nums[j]=nums[next];
j=next;
next = 2*j+1; //2*j+1, 2*j+2 are the children of j
}
nums[j]=val;
}
return nums;
}