September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,19 @@
fcn heapSort(a){ // in place
n := a.len();
foreach start in ([(n-2)/2 .. 0,-1])
{ siftDown(a, start, n-1) }
foreach end in ([n-1 .. 1,-1]){
a.swap(0, end);
siftDown(a, 0, end-1);
}
a
}
fcn siftDown(a, start, end){
while((child := start*2 + 1) <= end){
if(child < end and a[child]<a[child+1]) child+=1;
if(a[start] >= a[child]) return();
a.swap(start, child);
start = child;
}
}

View file

@ -0,0 +1,2 @@
heapSort(L(170, 45, 75, -90, -802, 24, 2, 66)).println();
heapSort("this is a test".split("")).println();