Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,6 +0,0 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
type Index_Type is (<>);
|
||||
type Collection is array(Index_Type range <>) of Element_Type;
|
||||
with function "<" (Left, right : element_type) return boolean is <>;
|
||||
procedure Generic_Heapsort(Item : in out Collection);
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
procedure Generic_Heapsort(Item : in out Collection) is
|
||||
procedure Swap(Left : in out Element_Type; Right : in out Element_Type) is
|
||||
Temp : Element_Type := Left;
|
||||
begin
|
||||
Left := Right;
|
||||
Right := Temp;
|
||||
end Swap;
|
||||
procedure Sift_Down(Item : in out Collection) is
|
||||
Root : Integer := Index_Type'Pos(Item'First);
|
||||
Child : Integer := Index_Type'Pos(Item'Last);
|
||||
Last : Integer := Index_Type'Pos(Item'Last);
|
||||
begin
|
||||
while Root * 2 + 1 <= Last loop
|
||||
Child := Root * 2 + 1;
|
||||
if Child + 1 <= Last and then Item(index_Type'Val(Child)) < Item(Index_Type'Val(Child + 1)) then
|
||||
Child := Child + 1;
|
||||
end if;
|
||||
if Item(Index_Type'Val(Root)) < Item(Index_Type'Val(Child)) then
|
||||
Swap(Item(Index_Type'Val(Root)), Item(Index_Type'Val(Child)));
|
||||
Root := Child;
|
||||
else
|
||||
exit;
|
||||
end if;
|
||||
end loop;
|
||||
end Sift_Down;
|
||||
|
||||
procedure Heapify(Item : in out Collection) is
|
||||
First_Pos : Integer := Index_Type'Pos(Index_Type'First);
|
||||
Last_Pos : Integer := Index_Type'Pos(Index_type'Last);
|
||||
Start : Index_type := Index_Type'Val((Last_Pos - First_Pos + 1) / 2);
|
||||
begin
|
||||
loop
|
||||
Sift_Down(Item(Start..Item'Last));
|
||||
if Start > Index_Type'First then
|
||||
Start := Index_Type'Pred(Start);
|
||||
else
|
||||
exit;
|
||||
end if;
|
||||
end loop;
|
||||
end Heapify;
|
||||
Last_Index : Index_Type := Index_Type'Last;
|
||||
begin
|
||||
Heapify(Item);
|
||||
while Last_Index > Index_Type'First loop
|
||||
Swap(Item(Last_Index), Item(Item'First));
|
||||
Last_Index := Index_Type'Pred(Last_Index);
|
||||
Sift_Down(Item(Item'First..Last_Index));
|
||||
end loop;
|
||||
|
||||
end Generic_Heapsort;
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
with Generic_Heapsort;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Test_Generic_Heapsort is
|
||||
type Days is (Sun, Mon, Tue, Wed, Thu, Fri, Sat);
|
||||
type Days_Col is array(Days range <>) of Natural;
|
||||
procedure Sort is new Generic_Heapsort(Natural, Days, Days_Col);
|
||||
Week : Days_Col := (5, 2, 7, 3, 4, 9, 1);
|
||||
begin
|
||||
for I in Week'range loop
|
||||
Put(Days'Image(I) & ":" & Natural'Image(Week(I)) & " ");
|
||||
end loop;
|
||||
New_Line;
|
||||
Sort(Week);
|
||||
for I in Week'range loop
|
||||
Put(Days'Image(I) & ":" & Natural'Image(Week(I))& " ");
|
||||
end loop;
|
||||
New_Line;
|
||||
end Test_Generic_Heapsort;
|
||||
|
|
@ -6,13 +6,13 @@ siftDown: function [items, start, ending][
|
|||
if and? ending > child + 1
|
||||
a\[child+1] > a\[child] -> child: child + 1
|
||||
|
||||
if? a\[root] < a\[child][
|
||||
switch a\[root] < a\[child][
|
||||
tmp: a\[child]
|
||||
a\[child]: a\[root]
|
||||
a\[root]: tmp
|
||||
root: child
|
||||
]
|
||||
else -> return a
|
||||
-> return a
|
||||
]
|
||||
return a
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,81 +0,0 @@
|
|||
>>SOURCE FORMAT FREE
|
||||
*> This code is dedicated to the public domain
|
||||
*> This is GNUCOBOL 2.0
|
||||
identification division.
|
||||
program-id. heapsort.
|
||||
environment division.
|
||||
configuration section.
|
||||
repository. function all intrinsic.
|
||||
data division.
|
||||
working-storage section.
|
||||
01 filler.
|
||||
03 a pic 99.
|
||||
03 a-start pic 99.
|
||||
03 a-end pic 99.
|
||||
03 a-parent pic 99.
|
||||
03 a-child pic 99.
|
||||
03 a-sibling pic 99.
|
||||
03 a-lim pic 99 value 10.
|
||||
03 array-swap pic 99.
|
||||
03 array occurs 10 pic 99.
|
||||
procedure division.
|
||||
start-heapsort.
|
||||
|
||||
*> fill the array
|
||||
compute a = random(seconds-past-midnight)
|
||||
perform varying a from 1 by 1 until a > a-lim
|
||||
compute array(a) = random() * 100
|
||||
end-perform
|
||||
|
||||
perform display-array
|
||||
display space 'initial array'
|
||||
|
||||
*>heapify the array
|
||||
move a-lim to a-end
|
||||
compute a-start = (a-lim + 1) / 2
|
||||
perform sift-down varying a-start from a-start by -1 until a-start = 0
|
||||
|
||||
perform display-array
|
||||
display space 'heapified'
|
||||
|
||||
*> sort the array
|
||||
move 1 to a-start
|
||||
move a-lim to a-end
|
||||
perform until a-end = a-start
|
||||
move array(a-end) to array-swap
|
||||
move array(a-start) to array(a-end)
|
||||
move array-swap to array(a-start)
|
||||
subtract 1 from a-end
|
||||
perform sift-down
|
||||
end-perform
|
||||
|
||||
perform display-array
|
||||
display space 'sorted'
|
||||
|
||||
stop run
|
||||
.
|
||||
sift-down.
|
||||
move a-start to a-parent
|
||||
perform until a-parent * 2 > a-end
|
||||
compute a-child = a-parent * 2
|
||||
compute a-sibling = a-child + 1
|
||||
if a-sibling <= a-end and array(a-child) < array(a-sibling)
|
||||
*> take the greater of the two
|
||||
move a-sibling to a-child
|
||||
end-if
|
||||
if a-child <= a-end and array(a-parent) < array(a-child)
|
||||
*> the child is greater than the parent
|
||||
move array(a-child) to array-swap
|
||||
move array(a-parent) to array(a-child)
|
||||
move array-swap to array(a-parent)
|
||||
end-if
|
||||
*> continue down the tree
|
||||
move a-child to a-parent
|
||||
end-perform
|
||||
.
|
||||
display-array.
|
||||
perform varying a from 1 by 1 until a > a-lim
|
||||
display space array(a) with no advancing
|
||||
end-perform
|
||||
.
|
||||
end program heapsort.
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
class HeapSort {
|
||||
@:generic
|
||||
private static function siftDown<T>(arr: Array<T>, start:Int, end:Int) {
|
||||
var root = start;
|
||||
while (root * 2 + 1 <= end) {
|
||||
var child = root * 2 + 1;
|
||||
if (child + 1 <= end && Reflect.compare(arr[child], arr[child + 1]) < 0)
|
||||
child++;
|
||||
if (Reflect.compare(arr[root], arr[child]) < 0) {
|
||||
var temp = arr[root];
|
||||
arr[root] = arr[child];
|
||||
arr[child] = temp;
|
||||
root = child;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@:generic
|
||||
public static function sort<T>(arr:Array<T>) {
|
||||
if (arr.length > 1)
|
||||
{
|
||||
var start = (arr.length - 2) >> 1;
|
||||
while (start > 0) {
|
||||
siftDown(arr, start - 1, arr.length - 1);
|
||||
start--;
|
||||
}
|
||||
}
|
||||
|
||||
var end = arr.length - 1;
|
||||
while (end > 0) {
|
||||
var temp = arr[end];
|
||||
arr[end] = arr[0];
|
||||
arr[0] = temp;
|
||||
siftDown(arr, 0, end - 1);
|
||||
end--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Main {
|
||||
static function main() {
|
||||
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
|
||||
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
|
||||
3.5, 0.0, -4.1, -9.5];
|
||||
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
|
||||
'be', 'self-evident', 'that', 'all',
|
||||
'men', 'are', 'created', 'equal'];
|
||||
Sys.println('Unsorted Integers: ' + integerArray);
|
||||
HeapSort.sort(integerArray);
|
||||
Sys.println('Sorted Integers: ' + integerArray);
|
||||
Sys.println('Unsorted Floats: ' + floatArray);
|
||||
HeapSort.sort(floatArray);
|
||||
Sys.println('Sorted Floats: ' + floatArray);
|
||||
Sys.println('Unsorted Strings: ' + stringArray);
|
||||
HeapSort.sort(stringArray);
|
||||
Sys.println('Sorted Strings: ' + stringArray);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,43 +1,34 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
with javascript_semantics
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">siftDown</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">arr</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">last</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">root</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">root</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">last</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">child</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">root</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">child</span><span style="color: #0000FF;"><</span><span style="color: #000000;">last</span> <span style="color: #008080;">and</span> <span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">child</span><span style="color: #0000FF;">]<</span><span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">child</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">child</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">root</span><span style="color: #0000FF;">]>=</span><span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">child</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">root</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">root</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">child</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">child</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmp</span>
|
||||
<span style="color: #000000;">root</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">child</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">arr</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function sift_down(sequence s, integer root, last)
|
||||
while root*2<=last do
|
||||
integer child = root*2
|
||||
if child<last and s[child]<s[child+1] then
|
||||
child += 1
|
||||
end if
|
||||
if s[root]>=s[child] then exit end if
|
||||
{s[root],s[child]} = extract(s,{child,root})
|
||||
root = child
|
||||
end while
|
||||
return s
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">heapify</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">arr</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">arr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">siftDown</span><span style="color: #0000FF;">(</span><span style="color: #000000;">arr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">arr</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function heapify(sequence s, integer last)
|
||||
for root=floor(last/2) to 1 by -1 do
|
||||
s = sift_down(s,root,last)
|
||||
end for
|
||||
return s
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">heap_sort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">arr</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">arr</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">arr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">heapify</span><span style="color: #0000FF;">(</span><span style="color: #000000;">arr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">last</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">last</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">last</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">arr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">last</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmp</span>
|
||||
<span style="color: #000000;">last</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">arr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">siftDown</span><span style="color: #0000FF;">(</span><span style="color: #000000;">arr</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">last</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">arr</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function heap_sort(sequence s)
|
||||
integer last = length(s)
|
||||
s = heapify(s,last)
|
||||
while last>1 do
|
||||
{s[1],s[last]} = extract(s,{last,1})
|
||||
last -= 1
|
||||
s = sift_down(s,1,last)
|
||||
end while
|
||||
return s
|
||||
end function
|
||||
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">heap_sort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"oranges"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"and"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"apples"</span><span style="color: #0000FF;">})</span>
|
||||
<!--
|
||||
?heap_sort({5,"oranges","and",3,"apples"})
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
function heapsort($a, $count) {
|
||||
$a = heapify $a $count
|
||||
$end = $count - 1
|
||||
while( $end -gt 0) {
|
||||
$a[$end], $a[0] = $a[0], $a[$end]
|
||||
$end--
|
||||
$a = siftDown $a 0 $end
|
||||
}
|
||||
$a
|
||||
}
|
||||
function heapify($a, $count) {
|
||||
$start = [Math]::Floor(($count - 2) / 2)
|
||||
while($start -ge 0) {
|
||||
$a = siftDown $a $start ($count-1)
|
||||
$start--
|
||||
}
|
||||
$a
|
||||
}
|
||||
function siftdown($a, $start, $end) {
|
||||
$b, $root = $true, $start
|
||||
while(( ($root * 2 + 1) -le $end) -and $b) {
|
||||
$child = $root * 2 + 1
|
||||
if( ($child + 1 -le $end) -and ($a[$child] -lt $a[$child + 1]) ) {
|
||||
$child++
|
||||
}
|
||||
if($a[$root] -lt $a[$child]) {
|
||||
$a[$root], $a[$child] = $a[$child], $a[$root]
|
||||
$root = $child
|
||||
}
|
||||
else { $b = $false}
|
||||
}
|
||||
$a
|
||||
}
|
||||
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11)
|
||||
"$(heapsort $array $array.Count)"
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
include Settings
|
||||
-- 23 Aug 2025
|
||||
include Setting
|
||||
|
||||
say 'HEAPSORT - 4 Mar 2025'
|
||||
say 'HEAPSORT'
|
||||
say version
|
||||
say
|
||||
call Generate
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue