Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
21
Task/Knuth-shuffle/UNIX-Shell/knuth-shuffle.sh
Normal file
21
Task/Knuth-shuffle/UNIX-Shell/knuth-shuffle.sh
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Shuffle array[@].
|
||||
function shuffle {
|
||||
integer i j t
|
||||
|
||||
((i = ${#array[@]}))
|
||||
while ((i > 1)); do
|
||||
((j = RANDOM)) # 0 <= j < 32768
|
||||
((j < 32768 % i)) && continue # no modulo bias
|
||||
((j %= i)) # 0 <= j < i
|
||||
|
||||
((i -= 1))
|
||||
((t = array[i]))
|
||||
((array[i] = array[j]))
|
||||
((array[j] = t))
|
||||
done
|
||||
}
|
||||
|
||||
# Test program.
|
||||
set -A array 11 22 33 44 55 66 77 88 99 110
|
||||
shuffle
|
||||
echo "${array[@]}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue