Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,6 @@
|
|||
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);
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
>>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.
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
local function sift_down(a, start, last)
|
||||
local root = start
|
||||
while root * 2 <= last do
|
||||
local child = root * 2
|
||||
if child < last and a[child] < a[child + 1] then child += 1 end
|
||||
if a[root] < a[child] then
|
||||
a[root], a[child] = a[child], a[root]
|
||||
root = child
|
||||
else
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function heapify(a, count)
|
||||
local start = count // 2
|
||||
while start > 0 do
|
||||
sift_down(a, start, count)
|
||||
start -= 1
|
||||
end
|
||||
end
|
||||
|
||||
local function heap_sort(a)
|
||||
local last = #a
|
||||
heapify(a, last)
|
||||
while last > 1 do
|
||||
a[1], a[last] = a[last], a[1]
|
||||
last -= 1
|
||||
sift_down(a, 1, last)
|
||||
end
|
||||
end
|
||||
|
||||
local array = { {4, 65, 2, -31, 0, 99, 2, 83, 782, 1}, {7, 5, 2, 6, 1, 4, 2, 6, 3} }
|
||||
for array as a do
|
||||
print($"Before: \{{a:concat(", ")}}")
|
||||
heap_sort(a)
|
||||
print($"After : \{{a:concat(", ")}}")
|
||||
print()
|
||||
end
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
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)"
|
||||
|
|
@ -0,0 +1,268 @@
|
|||
# riscv assembly raspberry pico2 rp2350
|
||||
# program heapsort.s
|
||||
# connexion putty com3
|
||||
/*********************************************/
|
||||
/* CONSTANTES */
|
||||
/********************************************/
|
||||
/* for this file see risc-v task include a file */
|
||||
.include "../../constantesRiscv.inc"
|
||||
|
||||
/****************************************************/
|
||||
/* MACROS */
|
||||
/****************************************************/
|
||||
#.include "../ficmacrosriscv.inc" # for debugging only
|
||||
|
||||
|
||||
/*******************************************/
|
||||
/* INITIALED DATAS */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessStart: .asciz "Program riscv start.\r\n"
|
||||
szMessEnd: .asciz "\nProgram end OK.\r\n"
|
||||
szCariageReturn: .asciz "\r\n"
|
||||
|
||||
szMessSortOk: .asciz "Area sorted.\n"
|
||||
szMessSortNok: .asciz "Area not sorted !!!!!.\n"
|
||||
szLibSort: .asciz "\nAfter sort\n"
|
||||
szSpace: .asciz " "
|
||||
|
||||
.align 2
|
||||
tabNumber: .int 5,7,8,3,2,9,1,4,6
|
||||
#tabNumber: .int 9,8,7,6,5,4,3,2,1
|
||||
.equ NBTABNUMBER1, . - tabNumber
|
||||
.equ NBTABNUMBER, NBTABNUMBER1 / 4 # compute items number
|
||||
|
||||
/*******************************************/
|
||||
/* UNINITIALED DATA */
|
||||
/*******************************************/
|
||||
.bss
|
||||
.align 2
|
||||
sConvArea: .skip 24
|
||||
|
||||
/********************************...-..--*****/
|
||||
/* SECTION CODE */
|
||||
/**********************************************/
|
||||
.text
|
||||
.global main
|
||||
|
||||
main:
|
||||
call stdio_init_all # général init
|
||||
1: # start loop connexion
|
||||
li a0,0 # raz argument register
|
||||
call tud_cdc_n_connected # waiting for USB connection
|
||||
beqz a0,1b # return code = zero ?
|
||||
|
||||
la a0,szMessStart # message address
|
||||
call writeString # display message
|
||||
|
||||
la s0,tabNumber # number array address
|
||||
li s1,NBTABNUMBER
|
||||
li s2,0
|
||||
2: # item loop
|
||||
sh2add t3,s2,s0 # compute item address
|
||||
lw a0,(t3)
|
||||
call displayResultD
|
||||
add s2,s2,1
|
||||
blt s2,s1,2b
|
||||
la a0,szCariageReturn
|
||||
call writeString
|
||||
mv a0,s0 # array address
|
||||
mv a1,s1 # array size
|
||||
call heapSort # sort
|
||||
mv a0,s0 # number array address
|
||||
mv a1,s1 # array size
|
||||
call isSorted
|
||||
|
||||
la a0,szLibSort
|
||||
call writeString
|
||||
|
||||
li s2,0
|
||||
3: # item loop
|
||||
sh2add t3,s2,s0 # compute item address
|
||||
lw a0,(t3)
|
||||
call displayResultD
|
||||
add s2,s2,1
|
||||
blt s2,s1,3b
|
||||
la a0,szCariageReturn
|
||||
call writeString
|
||||
la a0,szMessEnd
|
||||
call writeString
|
||||
call getchar
|
||||
100: # final loop
|
||||
j 100b
|
||||
/**********************************************/
|
||||
/* display Result décimal */
|
||||
/**********************************************/
|
||||
/* a0 value */
|
||||
.equ LGZONECONV, 20
|
||||
displayResultD:
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp)
|
||||
la a1,sConvArea # conversion result address
|
||||
call conversion10 # binary conversion
|
||||
la a0,sConvArea # message address
|
||||
call writeString # display message
|
||||
la a0,szSpace
|
||||
call writeString
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
|
||||
/**********************************************/
|
||||
/* sort control */
|
||||
/**********************************************/
|
||||
/* a0 array address */
|
||||
/* a1 array size */
|
||||
.equ LGZONECONV, 20
|
||||
isSorted:
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp)
|
||||
li t0,0
|
||||
sh2add t1,t0,a0
|
||||
lw t2,(t1) # load first element
|
||||
1:
|
||||
addi t0,t0,1
|
||||
blt t0,a1,2f # end indice ?
|
||||
la a0,szMessSortOk
|
||||
call writeString
|
||||
li a0,1 # yes -> area is sorted
|
||||
j 100f
|
||||
2:
|
||||
sh2add t1,t0,a0
|
||||
lw t3,(t1) # load next element
|
||||
bge t3,t2,3f # >= ?
|
||||
la a0,szMessSortNok
|
||||
call writeString
|
||||
li a0,0 # no -> area is not sorted
|
||||
j 100f
|
||||
3:
|
||||
mv t2,t3
|
||||
j 1b
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
|
||||
/******************************************************************/
|
||||
/* merge sort */
|
||||
/******************************************************************/
|
||||
/* a0 contains array address */
|
||||
/* a1 contains array size */
|
||||
heapSort:
|
||||
addi sp, sp, -8 # reserve stack
|
||||
sw ra, 0(sp) # save registers
|
||||
sw s0, 4(sp)
|
||||
mv s0,a1
|
||||
addi s0,s0,-1 # last element
|
||||
call heapify # first place table in max-heap order
|
||||
1:
|
||||
blez s0,100f
|
||||
li a1,0 #
|
||||
mv a2,s0
|
||||
call swapElement
|
||||
addi s0,s0,-1 # decrement indice
|
||||
li a1,0 #
|
||||
mv a2,s0
|
||||
call siftDown
|
||||
j 1b
|
||||
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
lw s0, 4(sp)
|
||||
addi sp, sp, 8
|
||||
ret
|
||||
/******************************************************************/
|
||||
/* place array in max-heap order */
|
||||
/******************************************************************/
|
||||
/* a0 contains array address */
|
||||
/* a1 element number */
|
||||
|
||||
heapify: # INFO: heapify
|
||||
addi sp, sp, -12 # reserve stack
|
||||
sw ra, 0(sp) # save registers
|
||||
sw s0, 4(sp)
|
||||
sw s1, 8(sp)
|
||||
mv s0,a1 # init
|
||||
addi t1,a1,-2
|
||||
srli s1,t1,1 # / by 2
|
||||
1:
|
||||
bltz s1,100f
|
||||
mv a1,s1
|
||||
addi a2,s0,-1
|
||||
call siftDown
|
||||
addi s1,s1,-1
|
||||
j 1b
|
||||
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
lw s0, 4(sp)
|
||||
lw s1, 8(sp)
|
||||
addi sp, sp, 12
|
||||
ret
|
||||
|
||||
/******************************************************************/
|
||||
/* put the heap back in max-heap order */
|
||||
/******************************************************************/
|
||||
/* a0 contains array address */
|
||||
/* a1 contains the first index */
|
||||
/* a2 contains the last index */
|
||||
siftDown: # INFO: siftDown
|
||||
addi sp, sp, -12 # reserve stack
|
||||
sw ra, 0(sp) # save registers
|
||||
sw s0, 4(sp)
|
||||
sw s1, 8(sp)
|
||||
mv s1,a2 # init
|
||||
1:
|
||||
slli s0,a1,1
|
||||
addi s0,s0,1
|
||||
bgt s0,s1,100f
|
||||
addi t1,s0,1
|
||||
bgt t1,s1,2f
|
||||
sh2add t2,s0,a0
|
||||
lw t3,(t2)
|
||||
sh2add t2,t1,a0
|
||||
lw t4,(t2)
|
||||
bge t3,t4,2f
|
||||
mv s0,t1
|
||||
2:
|
||||
sh2add t2,s0,a0 # compare elements on the table
|
||||
lw t3,(t2)
|
||||
sh2add t2,a1,a0 # root
|
||||
lw t4,(t2)
|
||||
bge t4,t3,100f
|
||||
mv a2,s0
|
||||
call swapElement
|
||||
mv a1,s0 # root = child
|
||||
j 1b
|
||||
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
lw s0, 4(sp)
|
||||
lw s1, 8(sp)
|
||||
addi sp, sp, 12
|
||||
ret
|
||||
/******************************************************************/
|
||||
/* swap values in array */
|
||||
/******************************************************************/
|
||||
/* a0 contains array address */
|
||||
/* a1 first indice */
|
||||
/* a2 second indice */
|
||||
swapElement: # INFO: swapElement
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp) # save registers
|
||||
sh2add t0,a1,a0
|
||||
lw t1,(t0)
|
||||
sh2add t2,a2,a0
|
||||
lw t3,(t2) # swap values
|
||||
sw t3,(t0)
|
||||
sw t1,(t2)
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
/************************************/
|
||||
/* file include Fonctions */
|
||||
/***********************************/
|
||||
/* for this file see risc-v task include a file */
|
||||
.include "../../includeFunctions.s"
|
||||
Loading…
Add table
Add a link
Reference in a new issue