September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -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()}")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue