We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e7180a1 commit 10ea1ebCopy full SHA for 10ea1eb
1 file changed
algorithms/sorting/heap_sort.m
@@ -0,0 +1,40 @@
1
+function list = heapSort(list)
2
+
3
+ function list = siftDown(list,root,theEnd)
4
+ while (root * 2) <= theEnd
5
6
+ child = root * 2;
7
+ if (child + 1 <= theEnd) && (list(child) < list(child+1))
8
+ child = child + 1;
9
+ end
10
11
+ if list(root) < list(child)
12
+ list([root child]) = list([child root]); %Swap
13
+ root = child;
14
+ else
15
+ return
16
17
18
+ end %while
19
+ end %siftDown
20
21
+ count = numel(list);
22
23
+ %Because heapify is called once in pseudo-code, it is inline here
24
+ start = floor(count/2);
25
26
+ while start >= 1
27
+ list = siftDown(list, start, count);
28
+ start = start - 1;
29
30
+ %End Heapify
31
32
+ while count > 1
33
34
+ list([count 1]) = list([1 count]); %Swap
35
+ count = count - 1;
36
+ list = siftDown(list,1,count);
37
38
39
40
+end
0 commit comments