September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -3,6 +3,8 @@
|
|||
;Task:
|
||||
Sort an array of positive integers using the [[wp:Bead_sort|Bead Sort Algorithm]].
|
||||
|
||||
A ''bead sort'' is also known as a ''gravity sort''.
|
||||
|
||||
|
||||
Algorithm has O(S), where S is the sum of the integers in the input set: Each bead is moved individually.
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
// version 1.1.2
|
||||
|
||||
fun beadSort(a: IntArray) {
|
||||
val n = a.size
|
||||
if (n < 2) return
|
||||
var max = a.max()!!
|
||||
val beads = ByteArray(max * n)
|
||||
/* mark the beads */
|
||||
for (i in 0 until n)
|
||||
for (j in 0 until a[i])
|
||||
beads[i * max + j] = 1
|
||||
|
||||
for (j in 0 until max) {
|
||||
/* count how many beads are on each post */
|
||||
var sum = 0
|
||||
for (i in 0 until n) {
|
||||
sum += beads[i * max + j]
|
||||
beads[i * max + j] = 0
|
||||
}
|
||||
/* mark bottom sum beads */
|
||||
for (i in n - sum until n) beads[i * max + j] = 1
|
||||
}
|
||||
|
||||
for (i in 0 until n) {
|
||||
var j = 0
|
||||
while (j < max && beads[i * max + j] == 1.toByte()) j++
|
||||
a[i] = j
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val a = intArrayOf(5, 3, 1, 7, 4, 1, 1, 20)
|
||||
println("Before sorting : ${a.contentToString()}")
|
||||
beadSort(a)
|
||||
println("After sorting : ${a.contentToString()}")
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
in='10 -12 1 0 999 8 2 2 4 4'
|
||||
Do i=1 To words(in)
|
||||
z.i=word(in,i)
|
||||
End
|
||||
n=i-1
|
||||
init=0
|
||||
Call minmax
|
||||
|
||||
beads.=0;
|
||||
Do i=1 To words(in)
|
||||
z=z.i
|
||||
beads.z+=1
|
||||
End
|
||||
j=0
|
||||
Do i=lo To hi
|
||||
Do While beads.i>0
|
||||
j+=1
|
||||
s.j=i
|
||||
beads.i-=1
|
||||
End;
|
||||
End;
|
||||
Call show ' Input:',z.,n
|
||||
Call show 'Sorted:',s.,n
|
||||
Exit
|
||||
|
||||
minmax:
|
||||
Do i=1 To n
|
||||
If init=0 Then Do
|
||||
init=1
|
||||
lo=z.i
|
||||
hi=z.i
|
||||
End
|
||||
Else Do
|
||||
lo=min(lo,z.i)
|
||||
hi=max(hi,z.i)
|
||||
End
|
||||
End
|
||||
Return
|
||||
|
||||
show: Procedure Expose n
|
||||
Use Arg txt,a.
|
||||
ol=txtg>
|
||||
Do i=1 To n
|
||||
ol=ol format(a.i,3)
|
||||
End
|
||||
Say ol
|
||||
Return
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
/*REXX program sorts a list of integers using a bead sort. */
|
||||
|
||||
/*get some grassHopper numbers. */
|
||||
grasshopper=,
|
||||
1 4 10 12 22 26 30 46 54 62 66 78 94 110 126 134 138 158 162 186 190 222 254 270
|
||||
|
||||
|
||||
|
||||
/*GreeenGrocer numbers are also called hexagonal pyramidal */
|
||||
/* numbers. */
|
||||
greengrocer=,
|
||||
0 4 16 40 80 140 224 336 480 660 880 1144 1456 1820 2240 2720 3264 3876 4560
|
||||
|
||||
|
||||
/*get some Bernoulli numerator numbers. */
|
||||
bernN='1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617 0 43867 0 -174611 0 854513'
|
||||
|
||||
|
||||
/*Psi is also called the Reduced Totient function, and */
|
||||
/* is also called Carmichale lambda, or LAMBDA function.*/
|
||||
psi=,
|
||||
1 1 2 2 4 2 6 2 6 4 10 2 12 6 4 4 16 6 18 4 6 10 22 2 20 12 18 6 28 4 30 8 10 16
|
||||
|
||||
|
||||
|
||||
list=grasshopper greengrocer bernN psi /*combine the four lists into one*/
|
||||
|
||||
|
||||
call showL 'before sort',list /*show list before sorting. */
|
||||
!=beadSort(list) /*invoke the bead sort. */
|
||||
call showL ' after sort',! /*show after array elements*/
|
||||
exit
|
||||
|
||||
|
||||
/*─────────────────────────────────beadSort@ subroutine────────────*/
|
||||
beadSort: procedure expose _.
|
||||
parse arg z
|
||||
!='' /*this'll be the sorted list*/
|
||||
low=999999999; high=-low /*define the low and high #s*/
|
||||
_.=0 /*define all beads to zero. */
|
||||
|
||||
|
||||
do j=1 until z=='' /*pick the meat off the bone*/
|
||||
parse var z x z
|
||||
if \datatype(x,'Whole') then
|
||||
do
|
||||
say
|
||||
say '*** error! ***'
|
||||
say
|
||||
say 'element' j "in list isn't numeric:" x
|
||||
say
|
||||
exit 13
|
||||
end
|
||||
|
||||
x=x/1 /*normalize number, it could*/
|
||||
/*be: +4 007 5. 2e3 etc.*/
|
||||
_.x=_.x+1 /*indicate this bead has a #*/
|
||||
low=min(low,x) /*keep track of the lowest #*/
|
||||
high=max(high,x) /* " " " " highest#*/
|
||||
end j
|
||||
|
||||
/*now, collect the beads and*/
|
||||
do m=low to high /*let them fall (to zero). */
|
||||
if _.m==0 then iterate /*No bead here? Keep looking*/
|
||||
do n=1 for _.m /*let the beads fall to 0. */
|
||||
!=! m /*add it to the sorted list.*/
|
||||
end n
|
||||
end m
|
||||
|
||||
return !
|
||||
|
||||
|
||||
/*─────────────────────────────────────SHOW@ subroutine────────────*/
|
||||
showL:
|
||||
widthH=length(words(arg(2))) /*maximum width of the index*/
|
||||
|
||||
do j=1 for words(arg(2))
|
||||
say 'element' right(j,widthH) arg(1)":" right(word(arg(2),j),10)
|
||||
end j
|
||||
|
||||
say copies('─',80) /*show a separator line. */
|
||||
return
|
||||
|
|
@ -1,44 +1,31 @@
|
|||
/*REXX program sorts a list of integers using the bead sort algorithm. */
|
||||
grasshopper=, /*define two dozen grasshopper numbers.*/
|
||||
1 4 10 12 22 26 30 46 54 62 66 78 94 110 126 134 138 158 162 186 190 222 254 270
|
||||
|
||||
/*Green Grocer numbers are also called hexagonal pyramidal numbers.*/
|
||||
/* [↑] define two dozen grasshopper numbers. */
|
||||
gHopper= 1 4 10 12 22 26 30 46 54 62 66 78 94 110 126 134 138 158 162 186 190 222 254 270
|
||||
/* [↑] these are also called hexagonal pyramidal #s. */
|
||||
greenGrocer= 0 4 16 40 80 140 224 336 480 660 880 1144 1456 1820 2240 2720 3264 3876 4560
|
||||
|
||||
/*define 23 Bernoulli numerator numbers*/
|
||||
bernN= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617 0 43867 0 -174611 0 854513'
|
||||
|
||||
/*Psi is also called the Reduced Totient function, and is*/
|
||||
psi=, /*also called Carmichael lambda, or the LAMBDA function.*/
|
||||
1 1 2 2 4 2 6 2 6 4 10 2 12 6 4 4 16 6 18 4 6 10 22 2 20 12 18 6 28 4 30 8 10 16
|
||||
|
||||
#= grasshopper greenGrocer bernN psi /*combine the four lists into one list.*/
|
||||
call show 'before sort', # /*display the list before sorting. */
|
||||
call show ' after sort', beadSort(#) /* " " " after " */
|
||||
/*define twenty-three Bernoulli numerator numbers. */
|
||||
bernN= '1 -1 1 0 -1 0 1 0 -1 0 5 0 -691 0 7 0 -3617 0 43867 0 -174611 0'
|
||||
/* [↑] also called the Reduced Totient function, and is*/
|
||||
/*also called Carmichael lambda, or the LAMBDA function*/
|
||||
psi= 1 1 2 2 4 2 6 2 6 4 10 2 12 6 4 4 16 6 18 4 6 10 22 2 20 12 18 6 28 4 30 8 10 16
|
||||
y= gHopper greenGrocer bernN psi /*combine the four lists into one list.*/
|
||||
call show 'before sort', y /*display the list before sorting. */
|
||||
say copies('░', 75) /*show long separator line before sort.*/
|
||||
call show ' after sort', beadSort(y) /*display the list after sorting. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
beadSort: procedure; parse arg low . 1 high . 1 z,$ /*$: the list to be sorted. */
|
||||
@.=0 /*set all beads (@.) to zero.*/
|
||||
do j=1 until z==''; parse var z x z /*pick the meat off the bone.*/
|
||||
if \datatype(x, 'W') then do; say '***error***'
|
||||
say 'element' j "in list isn't numeric:" x
|
||||
say; exit 13
|
||||
end /* [↑] exit pgm with RC=13. */
|
||||
x=x/1 /*normalize: 4. 004 +4 .4e0 */
|
||||
@.x=@.x+1 /*indicate this bead has a #.*/
|
||||
low=min(low,x); high=max(high,x) /*track lowest and highest #.*/
|
||||
end /*j*/
|
||||
beadSort: procedure; parse arg low . 1 high . 1 z,$; @.=0 /*$: the list to be sorted. */
|
||||
do j=1 until z==''; parse var z x z /*pick the meat off the bone.*/
|
||||
x=x/1; @.x=@.x+1 /*normalize X; bump counter.*/
|
||||
low=min(low, x); high=max(high, x) /*track lowest and highest #.*/
|
||||
end /*j*/
|
||||
/* [↓] now, collect beads and*/
|
||||
do m=low to high /*let them fall (to zero). */
|
||||
if @.m\==0 then do n=1 for @.m; $=$ m /*have we found a bead here? */
|
||||
end /*n*/ /* [↑] add it to sorted list*/
|
||||
end /*m*/
|
||||
|
||||
do m=low to high; do @.m; $=$ m; end /*let them fall (to zero). */
|
||||
end /*m*/
|
||||
return $
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show: parse arg txt,y; _=left('', 20)
|
||||
w=length(words(y)); do k=1 for words(y) /* [↑] twenty pad blanks. */
|
||||
say _ 'element' right(k, w) txt":" right(word(y, k), 9)
|
||||
end /*k*/
|
||||
say copies('─', 70) /*show a long separator line.*/
|
||||
show: parse arg txt,y; w=length( words(y) )
|
||||
do k=1 for words(y) /* [↑] twenty pad blanks. */
|
||||
say right('element',30) right(k,w) txt":" right(word(y,k),9)
|
||||
end /*k*/
|
||||
return
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
fcn columns(m){ // m is list of lists of zeros/beads, # beads is n, eg (0,0,0)==3
|
||||
m
|
||||
.apply("len") // (0,0,0)-->3
|
||||
.reduce("max") // largest bead stack
|
||||
.walker() // [0..max]
|
||||
.apply('wrap(i){ m.filter('wrap(s){ s.len() > i }).len().pump(List,0) });
|
||||
}
|
||||
|
||||
fcn beadSort(data){
|
||||
data.apply("pump",List,0):columns(_):columns(_).apply("len");
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
T(5,3,1,7,4,1,1):beadSort(_).println();
|
||||
T(4,3,3,2,1):beadSort(_).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue