September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -2,18 +2,17 @@
|
|||
' compile with: fbc -s console
|
||||
' for boundary checks on array's compile with: fbc -s console -exx
|
||||
|
||||
' sort from lower bound to the highter bound
|
||||
' sort from lower bound to the higher bound
|
||||
' array's can have subscript range from -2147483648 to +2147483647
|
||||
|
||||
Sub siftdown(hs() As Long, start As ULong , end_ As ULong)
|
||||
|
||||
Sub siftdown(hs() As Long, start As ULong, end_ As ULong)
|
||||
Dim As ULong root = start
|
||||
Dim As Long lb = LBound(hs)
|
||||
|
||||
While root * 2 +1 < end_
|
||||
Dim As ULong child = root * 2 +1
|
||||
If (child +1 < end_) And (hs(lb + child) < hs(lb + child +1)) Then
|
||||
child = child +1
|
||||
While root * 2 + 1 <= end_
|
||||
Dim As ULong child = root * 2 + 1
|
||||
If (child + 1 <= end_) AndAlso (hs(lb + child) < hs(lb + child + 1)) Then
|
||||
child = child + 1
|
||||
End If
|
||||
If hs(lb + root) < hs(lb + child) Then
|
||||
Swap hs(lb + root), hs(lb + child)
|
||||
|
|
@ -22,27 +21,24 @@ Sub siftdown(hs() As Long, start As ULong , end_ As ULong)
|
|||
Return
|
||||
End If
|
||||
Wend
|
||||
|
||||
End Sub
|
||||
|
||||
Sub heapsort(hs() As Long)
|
||||
|
||||
Dim As Long lb = LBound(hs)
|
||||
Dim As ULong count = UBound(hs) - lb
|
||||
Dim As Long start = (count -2) \ 2
|
||||
Dim As ULong count = UBound(hs) - lb + 1
|
||||
Dim As Long start = (count - 2) \ 2
|
||||
Dim As ULong end_ = count - 1
|
||||
|
||||
While start >= 0
|
||||
siftdown(hs(), start, count)
|
||||
start = start -1
|
||||
siftdown(hs(), start, end_)
|
||||
start = start - 1
|
||||
Wend
|
||||
|
||||
Dim As ULong end_ = count
|
||||
While end_ > 0
|
||||
Swap hs(lb + end_), hs(lb)
|
||||
end_ = end_ - 1
|
||||
siftdown(hs(), 0, end_)
|
||||
end_ = end_ -1
|
||||
Wend
|
||||
|
||||
End Sub
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
|
@ -53,7 +49,7 @@ Dim As Long i, lb = LBound(array), ub = UBound(array)
|
|||
Randomize Timer
|
||||
For i = lb To ub : array(i) = i : Next
|
||||
For i = lb To ub
|
||||
Swap array(i), array(Int(Rnd * (ub - lb +1)) + lb)
|
||||
Swap array(i), array(Int(Rnd * (ub - lb + 1)) + lb)
|
||||
Next
|
||||
|
||||
Print "Unsorted"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
function swap(data, i, j) {
|
||||
var tmp = data[i];
|
||||
data[i] = data[j];
|
||||
data[j] = tmp;
|
||||
}
|
||||
|
||||
function heap_sort(arr) {
|
||||
put_array_in_heap_order(arr);
|
||||
end = arr.length - 1;
|
||||
while(end > 0) {
|
||||
swap(arr, 0, end);
|
||||
sift_element_down_heap(arr, 0, end);
|
||||
end -= 1
|
||||
}
|
||||
}
|
||||
|
||||
function put_array_in_heap_order(arr) {
|
||||
var i;
|
||||
i = arr.length / 2 - 1;
|
||||
i = Math.floor(i);
|
||||
while (i >= 0) {
|
||||
sift_element_down_heap(arr, i, arr.length);
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
function sift_element_down_heap(heap, i, max) {
|
||||
var i_big, c1, c2;
|
||||
while(i < max) {
|
||||
i_big = i;
|
||||
c1 = 2*i + 1;
|
||||
c2 = c1 + 1;
|
||||
if (c1 < max && heap[c1] > heap[i_big])
|
||||
i_big = c1;
|
||||
if (c2 < max && heap[c2] > heap[i_big])
|
||||
i_big = c2;
|
||||
if (i_big == i) return;
|
||||
swap(heap,i, i_big);
|
||||
i = i_big;
|
||||
}
|
||||
}
|
||||
|
||||
arr = [12, 11, 15, 10, 9, 1, 2, 3, 13, 14, 4, 5, 6, 7, 8,];
|
||||
heap_sort(arr);
|
||||
alert(arr);
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
// version 1.1.0
|
||||
|
||||
fun heapSort(a: IntArray) {
|
||||
heapify(a)
|
||||
var end = a.size - 1
|
||||
while (end > 0) {
|
||||
val temp = a[end]
|
||||
a[end] = a[0]
|
||||
a[0] = temp
|
||||
end--
|
||||
siftDown(a, 0, end)
|
||||
}
|
||||
}
|
||||
|
||||
fun heapify(a: IntArray) {
|
||||
var start = (a.size - 2) / 2
|
||||
while (start >= 0) {
|
||||
siftDown(a, start, a.size - 1)
|
||||
start--
|
||||
}
|
||||
}
|
||||
|
||||
fun siftDown(a: IntArray, start: Int, end: Int) {
|
||||
var root = start
|
||||
while (root * 2 + 1 <= end) {
|
||||
var child = root * 2 + 1
|
||||
if (child + 1 <= end && a[child] < a[child + 1]) child++
|
||||
if (a[root] < a[child]) {
|
||||
val temp = a[root]
|
||||
a[root] = a[child]
|
||||
a[child] = temp
|
||||
root = child
|
||||
}
|
||||
else return
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val aa = arrayOf(
|
||||
intArrayOf(100, 2, 56, 200, -52, 3, 99, 33, 177, -199),
|
||||
intArrayOf(4, 65, 2, -31, 0, 99, 2, 83, 782, 1),
|
||||
intArrayOf(12, 11, 15, 10, 9, 1, 2, 3, 13, 14, 4, 5, 6, 7, 8)
|
||||
)
|
||||
for (a in aa) {
|
||||
heapSort(a)
|
||||
println(a.joinToString(", "))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
fn main() {
|
||||
let mut v = [4,6,8,1,0,3,2,2,9,5];
|
||||
heap_sort(&mut v, |x,y| x < y);
|
||||
println!("{:?}", v);
|
||||
}
|
||||
|
||||
fn heap_sort<T,F>(array: &mut [T], order: F)
|
||||
where F: Fn(&T,&T) -> bool
|
||||
{
|
||||
let len = array.len();
|
||||
// Create heap
|
||||
for start in (0..len/2).rev() {
|
||||
sift_down(array,&order,start,len-1)
|
||||
}
|
||||
|
||||
for end in (1..len).rev() {
|
||||
array.swap(0,end);
|
||||
sift_down(array,&order,0,end-1)
|
||||
}
|
||||
}
|
||||
|
||||
fn sift_down<T,F>(array: &mut [T], order: &F, start: usize, end: usize)
|
||||
where F: Fn(&T,&T) -> bool
|
||||
{
|
||||
let mut root = start;
|
||||
loop {
|
||||
let mut child = root * 2 + 1;
|
||||
if child > end { break; }
|
||||
if child + 1 <= end && order(&array[child], &array[child + 1]) {
|
||||
child += 1;
|
||||
}
|
||||
if order(&array[root], &array[child]) {
|
||||
array.swap(root,child);
|
||||
root = child
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
use std::collections::BinaryHeap;
|
||||
|
||||
fn main() {
|
||||
let src = vec![6,2,3,6,1,2,7,8,3,2];
|
||||
let sorted= BinaryHeap::from(src).into_sorted_vec();
|
||||
println!("{:?}", sorted);
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
import <Utilities/Sequence.sl>;
|
||||
|
||||
TUPLE<T> ::= (A: T, B: T);
|
||||
|
||||
heapSort(x(1)) :=
|
||||
let
|
||||
heapified := heapify(x, (size(x) - 2) / 2 + 1);
|
||||
in
|
||||
sortLoop(heapified, size(heapified));
|
||||
|
||||
heapify(x(1), i) :=
|
||||
x when i <= 0 else
|
||||
heapify(siftDown(x, i, size(x)), i - 1);
|
||||
|
||||
sortLoop(x(1), i) :=
|
||||
x when i <= 2 else
|
||||
sortLoop( siftDown(swap(x, 1, i), 1, i - 1), i - 1);
|
||||
|
||||
siftDown(x(1), start, end) :=
|
||||
let
|
||||
child := start * 2;
|
||||
child1 := child + 1 when child + 1 <= end and x[child] < x[child + 1] else child;
|
||||
in
|
||||
x when child >= end else
|
||||
x when x[start] >= x[child1] else
|
||||
siftDown(swap(x, child1, start), child1, end);
|
||||
|
||||
swap(list(1), i, j) :=
|
||||
let
|
||||
vals := (A: list[i], B: list[j]);
|
||||
in
|
||||
setElementAt(setElementAt(list, i, vals.B), j, vals.A);
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
Sub SiftDown(list() As Integer, start As Long, eend As Long)
|
||||
Dim root As Long : root = start
|
||||
Dim lb As Long : lb = LBound(list)
|
||||
Dim temp As Integer
|
||||
|
||||
While root * 2 + 1 <= eend
|
||||
Dim child As Long : child = root * 2 + 1
|
||||
If child + 1 <= eend Then
|
||||
If list(lb + child) < list(lb + child + 1) Then
|
||||
child = child + 1
|
||||
End If
|
||||
End If
|
||||
If list(lb + root) < list(lb + child) Then
|
||||
temp = list(lb + root)
|
||||
list(lb + root) = list(lb + child)
|
||||
list(lb + child) = temp
|
||||
|
||||
root = child
|
||||
Else
|
||||
Exit Sub
|
||||
End If
|
||||
Wend
|
||||
End Sub
|
||||
|
||||
Sub HeapSort(list() As Integer)
|
||||
Dim lb As Long : lb = LBound(list)
|
||||
Dim count As Long : count = UBound(list) - lb + 1
|
||||
Dim start As Long : start = (count - 2) \ 2
|
||||
Dim eend As Long : eend = count - 1
|
||||
|
||||
While start >= 0
|
||||
SiftDown list(), start, eend
|
||||
start = start - 1
|
||||
Wend
|
||||
|
||||
Dim temp As Integer
|
||||
|
||||
While eend > 0
|
||||
temp = list(lb + eend)
|
||||
list(lb + eend) = list(lb)
|
||||
list(lb) = temp
|
||||
|
||||
eend = eend - 1
|
||||
|
||||
SiftDown list(), 0, eend
|
||||
Wend
|
||||
End Sub
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
heapSort(L(170, 45, 75, -90, -802, 24, 2, 66)).println();
|
||||
heapSort("this is a test".split("")).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue