Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,9 @@
-----------------------------------------------------------------------
-- Generic Quick_Sort procedure
-----------------------------------------------------------------------
generic
type Element is private;
type Index is (<>);
type Element_Array is array(Index range <>) of Element;
with function "<" (Left, Right : Element) return Boolean is <>;
procedure Quick_Sort(A : in out Element_Array);

View file

@ -0,0 +1,44 @@
-----------------------------------------------------------------------
-- Generic Quick_Sort procedure
-----------------------------------------------------------------------
procedure Quick_Sort (A : in out Element_Array) is
procedure Swap(Left, Right : Index) is
Temp : Element := A (Left);
begin
A (Left) := A (Right);
A (Right) := Temp;
end Swap;
begin
if A'Length > 1 then
declare
Pivot_Value : Element := A (A'First);
Right : Index := A'Last;
Left : Index := A'First;
begin
loop
while Left < Right and not (Pivot_Value < A (Left)) loop
Left := Index'Succ (Left);
end loop;
while Pivot_Value < A (Right) loop
Right := Index'Pred (Right);
end loop;
exit when Right <= Left;
Swap (Left, Right);
Left := Index'Succ (Left);
Right := Index'Pred (Right);
end loop;
if Right = A'Last then
Right := Index'Pred (Right);
Swap (A'First, A'Last);
end if;
if Left = A'First then
Left := Index'Succ (Left);
end if;
Quick_Sort (A (A'First .. Right));
Quick_Sort (A (Left .. A'Last));
end;
end if;
end Quick_Sort;

View file

@ -0,0 +1,32 @@
with Ada.Text_Io;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Quick_Sort;
procedure Sort_Test is
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
type Sales is array (Days range <>) of Float;
procedure Sort_Days is new Quick_Sort(Float, Days, Sales);
procedure Print (Item : Sales) is
begin
for I in Item'range loop
Put(Item => Item(I), Fore => 5, Aft => 2, Exp => 0);
end loop;
end Print;
Weekly_Sales : Sales := (Mon => 300.0,
Tue => 700.0,
Wed => 800.0,
Thu => 500.0,
Fri => 200.0,
Sat => 100.0,
Sun => 900.0);
begin
Print(Weekly_Sales);
Ada.Text_Io.New_Line(2);
Sort_Days(Weekly_Sales);
Print(Weekly_Sales);
end Sort_Test;

View file

@ -0,0 +1,66 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. quicksort RECURSIVE.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 temp PIC S9(8).
01 pivot PIC S9(8).
01 left-most-idx PIC 9(5).
01 right-most-idx PIC 9(5).
01 left-idx PIC 9(5).
01 right-idx PIC 9(5).
LINKAGE SECTION.
78 Arr-Length VALUE 50.
01 arr-area.
03 arr PIC S9(8) OCCURS Arr-Length TIMES.
01 left-val PIC 9(5).
01 right-val PIC 9(5).
PROCEDURE DIVISION USING REFERENCE arr-area, OPTIONAL left-val,
OPTIONAL right-val.
IF left-val IS OMITTED OR right-val IS OMITTED
MOVE 1 TO left-most-idx, left-idx
MOVE Arr-Length TO right-most-idx, right-idx
ELSE
MOVE left-val TO left-most-idx, left-idx
MOVE right-val TO right-most-idx, right-idx
END-IF
IF right-most-idx - left-most-idx < 1
GOBACK
END-IF
COMPUTE pivot = arr ((left-most-idx + right-most-idx) / 2)
PERFORM UNTIL left-idx > right-idx
PERFORM VARYING left-idx FROM left-idx BY 1
UNTIL arr (left-idx) >= pivot
END-PERFORM
PERFORM VARYING right-idx FROM right-idx BY -1
UNTIL arr (right-idx) <= pivot
END-PERFORM
IF left-idx <= right-idx
MOVE arr (left-idx) TO temp
MOVE arr (right-idx) TO arr (left-idx)
MOVE temp TO arr (right-idx)
ADD 1 TO left-idx
SUBTRACT 1 FROM right-idx
END-IF
END-PERFORM
CALL "quicksort" USING REFERENCE arr-area,
CONTENT left-most-idx, right-idx
CALL "quicksort" USING REFERENCE arr-area, CONTENT left-idx,
right-most-idx
GOBACK
.

View file

@ -0,0 +1,10 @@
(require 'seq)
(defun quicksort (xs)
(if (null xs)
()
(let* ((head (car xs))
(tail (cdr xs))
(lower-part (quicksort (seq-filter (lambda (x) (<= x head)) tail)))
(higher-part (quicksort (seq-filter (lambda (x) (> x head)) tail))))
(append lower-part (list head) higher-part))))

View file

@ -0,0 +1,19 @@
import gleam/int
import gleam/list
import gleam/order.{type Order, Lt}
pub fn quick_sort(xs: List(a), compare: fn(a, a) -> Order) -> List(a) {
case xs {
[] -> []
[x, ..xs] -> {
let #(left, right) = list.partition(xs, fn(y) { compare(y, x) == Lt })
let ql = quick_sort(left, compare)
let qr = quick_sort(right, compare)
list.append(list.append(ql, [x]), qr)
}
}
}
pub fn main() {
[31, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8] |> quick_sort(int.compare) |> echo
}

View file

@ -0,0 +1,9 @@
def qsort(x) {
switch(x) {
[],[_] -> x
[h,*t] -> qsort(t.filter{it <= h}) + [h] + qsort(t.filter{it > h})
}
}
def list = 10.map{ random(20) }
println qsort(list)

View file

@ -0,0 +1,12 @@
local array = {
{4, 65, 2, -31, 0, 99, 2, 83, 782, 1},
{7, 5, 2, 6, 1, 4, 2, 6, 3},
{"echo", "lima", "charlie", "whiskey", "golf", "papa", "alfa", "india", "foxtrot", "kilo"}
}
for array as a do
print($"Before: \{{a:concat(", ")}}")
a:sort()
print($"After : \{{a:concat(", ")}}")
print()
end

View file

@ -0,0 +1,52 @@
Function SortThree( [Array] $data )
{
if( $data[ 0 ] -gt $data[ 1 ] )
{
if( $data[ 0 ] -lt $data[ 2 ] )
{
$data = $data[ 1, 0, 2 ]
} elseif ( $data[ 1 ] -lt $data[ 2 ] ){
$data = $data[ 1, 2, 0 ]
} else {
$data = $data[ 2, 1, 0 ]
}
} else {
if( $data[ 0 ] -gt $data[ 2 ] )
{
$data = $data[ 2, 0, 1 ]
} elseif( $data[ 1 ] -gt $data[ 2 ] ) {
$data = $data[ 0, 2, 1 ]
}
}
$data
}
Function QuickSort( [Array] $data, $rand = ( New-Object Random ) )
{
$datal = $data.length
if( $datal -gt 3 )
{
[void] $datal--
$median = ( SortThree $data[ 0, ( $rand.Next( 1, $datal - 1 ) ), -1 ] )[ 1 ]
$lt = @()
$eq = @()
$gt = @()
$data | ForEach-Object { if( $_ -lt $median ) { $lt += $_ } elseif( $_ -eq $median ) { $eq += $_ } else { $gt += $_ } }
$lt = ( QuickSort $lt $rand )
$gt = ( QuickSort $gt $rand )
$data = @($lt) + $eq + $gt
} elseif( $datal -eq 3 ) {
$data = SortThree( $data )
} elseif( $datal -eq 2 ) {
if( $data[ 0 ] -gt $data[ 1 ] )
{
$data = $data[ 1, 0 ]
}
}
$data
}
QuickSort 5,3,1,2,4
QuickSort 'e','c','a','b','d'
QuickSort 0.5,0.3,0.1,0.2,0.4
$l = 100; QuickSort ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( 0, $l - 1 ) } )

View file

@ -0,0 +1,15 @@
function quicksort($array) {
$less, $equal, $greater = @(), @(), @()
if( $array.Count -gt 1 ) {
$pivot = $array[0]
foreach( $x in $array) {
if($x -lt $pivot) { $less += @($x) }
elseif ($x -eq $pivot) { $equal += @($x)}
else { $greater += @($x) }
}
$array = (@(quicksort $less) + @($equal) + @(quicksort $greater))
}
$array
}
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11)
"$(quicksort $array)"

View file

@ -0,0 +1,15 @@
function quicksort($in) {
$n = $in.count
switch ($n) {
0 {}
1 { $in[0] }
2 { if ($in[0] -lt $in[1]) {$in[0], $in[1]} else {$in[1], $in[0]} }
default {
$pivot = $in | get-random
$lt = $in | ? {$_ -lt $pivot}
$eq = $in | ? {$_ -eq $pivot}
$gt = $in | ? {$_ -gt $pivot}
@(quicksort $lt) + @($eq) + @(quicksort $gt)
}
}
}

View file

@ -0,0 +1,227 @@
# riscv assembly raspberry pico2 rp2350
# program quicksort.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,0
li s2,NBTABNUMBER
2: # item loop
sh2add t3,s1,s0 # compute item address
lw a0,(t3)
call displayResultD
add s1,s1,1
blt s1,s2,2b
la a0,szCariageReturn
call writeString
mv a0,s0 # number array address
li a1,0 # first item
addi a2,s2,-1 # last item
call quickSort # sort
mv a0,s0 # number array address
li a1,0 # first item
addi a2,s2,-1 # last item
call isSorted
la a0,szLibSort
call writeString
li s1,0
3: # item loop
sh2add t3,s1,s0 # compute item address
lw a0,(t3)
call displayResultD
add s1,s1,1
blt s1,s2,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
/**********************************************/
/* */
/**********************************************/
/* a0 area address */
/* a1 first element */
/* a2 last element */
.equ LGZONECONV, 20
isSorted:
addi sp, sp, -4 # reserve stack
sw ra, 0(sp)
mv t0,a1
sh2add t1,t0,a0
lw t2,(t1) # load first element
1:
addi t0,t0,1
ble t0,a2,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
/******************************************************************/
/* quick sort */
/******************************************************************/
/* a0 contains array address */
/* a1 contains the first element */
/* a2 contains the last element */
quickSort:
addi sp, sp, -16 # reserve stack
sw ra, 0(sp) # save registers
sw s0, 4(sp)
sw s1, 8(sp)
sw s2, 12(sp)
bge a1,a2,100f # first > last ? -> end
mv s0,a0 # save address area
mv s1,a1 # save first indice
mv s2,a2 # save last indice
call partition # partitioning
addi a2,a0,-1 # index partition - 1
mv a1,s1 # move first index
mv s1,a0 # save partition index
mv a0,s0 # array address
call quickSort # sort lower part
addi a1,s1,1 # partition index + 1
mv a2,s2 # last index
mv a0,s0
call quickSort # sort higter part
100:
lw ra, 0(sp)
lw s0, 4(sp)
lw s1, 8(sp)
lw s2, 12(sp)
addi sp, sp, 16
ret
/******************************************************************/
/* shell sort */
/******************************************************************/
/* a0 contains the address of table */
/* a1 contains index of first item */
/* a2 contains index of last item */
partition: # INFO: partition
addi sp, sp, -4 # reserve stack
sw ra, 0(sp) # save registers
mv t0,a1 # init with first index
mv t1,a1 # init with first index
sh2add t2,a2,a0
lw t3,(t2) # load value last index
1: # begin loop
sh2add t2,t1,a0
lw t4,(t2) # load value
bge t4,t3,2f # compare value 2
sh2add t2,t0,a0 # if value2 < value 1 -> swap values
lw t5,(t2)
sh2add t2,t0,a0
sw t4,(t2)
sh2add t2,t1,a0
sw t5,(t2)
addi t0,t0,1 # increment index
2:
addi t1,t1,1 # increment index 2
blt t1,a2,1b # < maxi -> loop
sh2add t2,t0,a0 # else swap value
lw t5,(t2)
sh2add t2,t0,a0
sw t3,(t2)
sh2add t2,a2,a0
sw t5,(t2)
mv a0,t0 # return index partition
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"

View file

@ -1,5 +1,7 @@
say "x" x 11 ~ " Benchmarking " ~ "x" x 11;
# run in a terminal: "zef install Benchmark" to install from https://raku.land
use Benchmark;
say "x" x 11 ~ " Benchmarking " ~ "x" x 11;
my $runs = 5;
my $elems = 10 * Kernel.cpu-cores * 2**10;
my @unsorted of Str = ('a'..'z').roll(8).join xx $elems;
@ -22,7 +24,7 @@ my %results = timethese $runs, {
my @metrics = <mean median sd>;
my $msg-row = "%.4f\t" x @metrics.elems ~ '%s';
say @metrics.join("\t");
for %results.kv -> $name, %m {
say sprintf($msg-row, %m{@metrics}, $name);
for %results.sort( *.value<median> ) {
once say @metrics.join("\t"); # header
say sprintf($msg-row, .value{@metrics}, .key)
}

View file

@ -0,0 +1,35 @@
Function quicksort(arr,s,n)
If n < 2 Then
Exit Function
End If
t = s + n - 1
l = s
r = t
p = arr(Int((l + r)/2))
Do Until l > r
Do While arr(l) < p
l = l + 1
Loop
Do While arr(r) > p
r = r -1
Loop
If l <= r Then
tmp = arr(l)
arr(l) = arr(r)
arr(r) = tmp
l = l + 1
r = r - 1
End If
Loop
If s < r Then
Call quicksort(arr,s,r-s+1)
End If
If l < t Then
Call quicksort(arr,l,t-l+1)
End If
quicksort = arr
End Function
myarray=Array(9,8,7,6,5,5,4,3,2,1,0,-1)
m = quicksort(myarray,0,12)
WScript.Echo Join(m,",")