Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,6 +1,6 @@
|
|||
beadSort: function [items][
|
||||
a: new items
|
||||
m: neg infinity
|
||||
m: neg infinite
|
||||
s: 0
|
||||
|
||||
loop a 'x [
|
||||
|
|
|
|||
|
|
@ -1,98 +0,0 @@
|
|||
>>SOURCE FORMAT FREE
|
||||
*> This code is dedicated to the public domain
|
||||
*> This is GNUCOBOL 2.0
|
||||
identification division.
|
||||
program-id. beadsort.
|
||||
environment division.
|
||||
configuration section.
|
||||
repository. function all intrinsic.
|
||||
data division.
|
||||
working-storage section.
|
||||
01 filler.
|
||||
03 row occurs 9 pic x(9).
|
||||
03 r pic 99.
|
||||
03 r1 pic 99.
|
||||
03 r2 pic 99.
|
||||
03 pole pic 99.
|
||||
03 a-lim pic 99 value 9.
|
||||
03 a pic 99.
|
||||
03 array occurs 9 pic 9.
|
||||
01 NL pic x value x'0A'.
|
||||
procedure division.
|
||||
start-beadsort.
|
||||
|
||||
*> fill the array
|
||||
compute a = random(seconds-past-midnight)
|
||||
perform varying a from 1 by 1 until a > a-lim
|
||||
compute array(a) = random() * 10
|
||||
end-perform
|
||||
|
||||
perform display-array
|
||||
display space 'initial array'
|
||||
|
||||
*> distribute the beads
|
||||
perform varying r from 1 by 1 until r > a-lim
|
||||
move all '.' to row(r)
|
||||
perform varying pole from 1 by 1 until pole > array(r)
|
||||
move 'o' to row(r)(pole:1)
|
||||
end-perform
|
||||
end-perform
|
||||
display NL 'initial beads'
|
||||
perform display-beads
|
||||
|
||||
*> drop the beads
|
||||
perform varying pole from 1 by 1 until pole > a-lim
|
||||
move a-lim to r2
|
||||
perform find-opening
|
||||
compute r1 = r2 - 1
|
||||
perform find-bead
|
||||
perform until r1 = 0 *> no bead or no opening
|
||||
*> drop the bead
|
||||
move '.' to row(r1)(pole:1)
|
||||
move 'o' to row(r2)(pole:1)
|
||||
*> continue up the pole
|
||||
compute r2 = r2 - 1
|
||||
perform find-opening
|
||||
compute r1 = r2 - 1
|
||||
perform find-bead
|
||||
end-perform
|
||||
end-perform
|
||||
display NL 'dropped beads'
|
||||
perform display-beads
|
||||
|
||||
*> count the beads in each row
|
||||
perform varying r from 1 by 1 until r > a-lim
|
||||
move 0 to array(r)
|
||||
inspect row(r) tallying array(r)
|
||||
for all 'o' before initial '.'
|
||||
end-perform
|
||||
|
||||
perform display-array
|
||||
display space 'sorted array'
|
||||
|
||||
stop run
|
||||
.
|
||||
find-opening.
|
||||
perform varying r2 from r2 by -1
|
||||
until r2 = 1 or row(r2)(pole:1) = '.'
|
||||
continue
|
||||
end-perform
|
||||
.
|
||||
find-bead.
|
||||
perform varying r1 from r1 by -1
|
||||
until r1 = 0 or row(r1)(pole:1) = 'o'
|
||||
continue
|
||||
end-perform
|
||||
.
|
||||
display-array.
|
||||
display space
|
||||
perform varying a from 1 by 1 until a > a-lim
|
||||
display space array(a) with no advancing
|
||||
end-perform
|
||||
.
|
||||
display-beads.
|
||||
perform varying r from 1 by 1 until r > a-lim
|
||||
display row(r)
|
||||
end-perform
|
||||
.
|
||||
end program beadsort.
|
||||
|
|
@ -1,17 +1,23 @@
|
|||
(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;">beadsort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">poles</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">poles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">poles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">a</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;">0</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">poles</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">poles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">poles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
function beadsort(sequence a)
|
||||
sequence poles = repeat(0,max(a))
|
||||
for k in a do
|
||||
poles[1..k] = sq_add(poles[1..k],1)
|
||||
--or
|
||||
-- for i,p in poles to k do
|
||||
-- poles[i] = p+1
|
||||
-- end for
|
||||
end for
|
||||
a[1..$] = 0
|
||||
for p in poles do
|
||||
a[-p..$] = sq_add(a[-p..$],1)
|
||||
--or
|
||||
-- for i,n in a from -p to -1 do
|
||||
-- a[i] = n+1
|
||||
-- end for
|
||||
end for
|
||||
return a
|
||||
end function
|
||||
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">beadsort</span><span style="color: #0000FF;">({</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">20</span><span style="color: #0000FF;">})</span>
|
||||
<!--
|
||||
?beadsort({5, 3, 1, 7, 4, 1, 1, 20})
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
Function BeadSort ( [Int64[]] $indata )
|
||||
{
|
||||
if( $indata.length -gt 1 )
|
||||
{
|
||||
$min = $indata[ 0 ]
|
||||
$max = $indata[ 0 ]
|
||||
for( $i = 1; $i -lt $indata.length; $i++ )
|
||||
{
|
||||
if( $indata[ $i ] -lt $min )
|
||||
{
|
||||
$min = $indata[ $i ]
|
||||
}
|
||||
if( $indata[ $i ] -gt $max ) {
|
||||
$max = $indata[ $i ]
|
||||
}
|
||||
} #Find the min & max
|
||||
$poles = New-Object 'UInt64[]' ( $max - $min + 1 )
|
||||
$indata | ForEach-Object {
|
||||
$min..$_ | ForEach-Object {
|
||||
$poles[ $_ - $min ] += 1
|
||||
}
|
||||
} #Add Beads to the poles, already moved to the bottom
|
||||
$min..( $max - 1 ) | ForEach-Object {
|
||||
$i = $_ - $min
|
||||
if( $poles[ $i ] -gt $poles[ $i + 1 ] )
|
||||
{ #No special case needed for min, since there will always be at least 1 = min
|
||||
( $poles[ $i ] )..( $poles[ $i + 1 ] + 1 ) | ForEach-Object {
|
||||
Write-Output ( $i + $min )
|
||||
}
|
||||
}
|
||||
} #Output the results in pipeline fashion
|
||||
1..( $poles[ $max - $min ] ) | ForEach-Object {
|
||||
Write-Output $max #No special case needed for max, since there will always be at least 1 = max
|
||||
}
|
||||
} else {
|
||||
Write-Output $indata
|
||||
}
|
||||
}
|
||||
|
||||
$l = 100; BeadSort ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( -( $l - 1 ), $l - 1 ) } )
|
||||
|
|
@ -1,10 +1,15 @@
|
|||
#!/bin/python3
|
||||
import random
|
||||
from itertools import zip_longest
|
||||
|
||||
# This is wrong, it works only on specific examples
|
||||
def beadsort(l):
|
||||
return list(map(sum, zip_longest(*[[1] * e for e in l], fillvalue=0)))
|
||||
def beadsort(l: list[int]) -> list[int]:
|
||||
transposed = list(map(sum, zip_longest(*[[1] * e for e in l], fillvalue=0)))
|
||||
return [sum(n > i for n in transposed) for i in range(len(l))]
|
||||
|
||||
def test_beadsort() -> None:
|
||||
for _ in range(100):
|
||||
ints = [random.randint(0, 50) for _ in range(random.randint(0, 10))]
|
||||
assert beadsort(ints) == sorted(ints, reverse=True)
|
||||
|
||||
# Demonstration code:
|
||||
print(beadsort([5,3,1,7,4,1,1]))
|
||||
if __name__ == "__main__":
|
||||
test_beadsort()
|
||||
print(beadsort([5, 3, 1, 7, 4, 1, 1]))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue