Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,98 @@
|
|||
>>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.
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
proc beadSort &a[] .
|
||||
for i to len a[] : max = higher max a[i]
|
||||
len beads[][] len a[]
|
||||
for i to len a[]
|
||||
len beads[i][] max
|
||||
for j to a[i] : beads[i][j] = 1
|
||||
.
|
||||
for j to max
|
||||
sum = 0
|
||||
for i = 1 to len a[]
|
||||
sum += beads[i][j]
|
||||
beads[i][j] = 0
|
||||
.
|
||||
for i = len a[] downto len a[] - sum + 1
|
||||
a[i] = j
|
||||
.
|
||||
.
|
||||
.
|
||||
a[] = [ 4 38 100 1 25 69 69 16 8 59 71 53 33 ]
|
||||
beadSort a[]
|
||||
print a[]
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
require "table2"
|
||||
|
||||
local function bead_sort(a)
|
||||
local res = {}
|
||||
local max = a:max()
|
||||
local trans = table.rep(max, 0)
|
||||
for a as i do
|
||||
for n = 1, i do trans[n] += 1 end
|
||||
end
|
||||
for a as _ do
|
||||
res:insert(trans:count(|n| -> n > 0))
|
||||
for n = 1, #trans do trans[n] -= 1 end
|
||||
end
|
||||
return res:reverse() -- return in ascending order
|
||||
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(", ")}}")
|
||||
local b = bead_sort(a)
|
||||
print($"After : \{{b:concat(", ")}}")
|
||||
print()
|
||||
end
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
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 ) } )
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
gen_abacus <- function(v){
|
||||
nmax <- max(v)
|
||||
gen_col <- function(n) c(rep(1, n), rep(0, nmax-n))
|
||||
sapply(v, gen_col)
|
||||
}
|
||||
|
||||
beadsort <- function(v){
|
||||
flip <- function(v) gen_abacus(v) |> apply(1, sum)
|
||||
v |> flip() |> flip() |> rev()
|
||||
}
|
||||
|
||||
beadsort(c(2, 4, 1, 3, 3))
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
# Bead Sort algorithm in Ring language
|
||||
aList = [5, 3, 1, 7, 4, 1, 1, 20]
|
||||
see "Original list: " + nl
|
||||
showList(aList)
|
||||
|
||||
aList = beadSort(aList)
|
||||
|
||||
see "Sorted list: " + nl
|
||||
showList(aList)
|
||||
|
||||
func beadSort aData
|
||||
nLen = len(aData)
|
||||
if nLen = 0 return aData ok
|
||||
|
||||
# Find largest element (number of columns)
|
||||
nMax = aData[1]
|
||||
for x in aData
|
||||
if x > nMax nMax = x ok
|
||||
next
|
||||
|
||||
# Initialize a matrix of beads (rows x columns)
|
||||
# In Ring, the lists are indexed from 1
|
||||
beads = list(nLen)
|
||||
for i = 1 to nLen
|
||||
beads[i] = list(nMax)
|
||||
for j = 1 to nMax
|
||||
if j <= aData[i]
|
||||
beads[i][j] = 1
|
||||
else
|
||||
beads[i][j] = 0
|
||||
ok
|
||||
next
|
||||
next
|
||||
|
||||
# Simulate gravity: column-wise summation and redistribution
|
||||
for j = 1 to nMax
|
||||
sum = 0
|
||||
for i = 1 to nLen
|
||||
sum += beads[i][j]
|
||||
beads[i][j] = 0
|
||||
next
|
||||
|
||||
# The beads "fall" to the bottom
|
||||
for i = nLen-sum+1 to nLen
|
||||
beads[i][j] = 1
|
||||
next
|
||||
next
|
||||
|
||||
# Extract result from matrix
|
||||
aResult = list(nLen)
|
||||
for i = 1 to nLen
|
||||
rowSum = 0
|
||||
for j = 1 to nMax
|
||||
rowSum += beads[i][j]
|
||||
next
|
||||
aResult[i] = rowSum
|
||||
next
|
||||
|
||||
return aResult
|
||||
|
||||
func showList aList
|
||||
for x in aList see "" + x + " " next
|
||||
see nl
|
||||
Loading…
Add table
Add a link
Reference in a new issue