September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,16 @@
(defparameter *shrink* 1.3)
(defun comb-sort (input)
(loop with input-size = (length input)
with gap = input-size
with swapped
do (when (> gap 1)
(setf gap (floor gap *shrink*)))
(setf swapped nil)
(loop for lo from 0
for hi from gap below input-size
when (> (aref input lo) (aref input hi))
do (rotatef (aref input lo) (aref input hi))
(setf swapped t))
while (or (> gap 1) swapped)
finally (return input)))

View file

@ -0,0 +1,21 @@
# v0.6
function combsort!(x::Array)::Array
gap, swaps = length(x), true
while gap > 1 || swaps
gap = floor(Int, gap / 1.25)
i, swaps = 0, false
while i + gap < length(x)
if x[i+1] > x[i+1+gap]
x[i+1], x[i+1+gap] = x[i+1+gap], x[i+1]
swaps = true
end
i += 1
end
end
return x
end
x = randn(100)
@show x combsort!(x)
@assert issorted(x)

View file

@ -0,0 +1,34 @@
// version 1.1.2
fun <T : Comparable<T>> combSort(input: Array<T>) {
var gap = input.size
if (gap <= 1) return // already sorted
var swaps = false
while (gap > 1 || swaps) {
gap = (gap / 1.247331).toInt()
if (gap < 1) gap = 1
var i = 0
swaps = false
while (i + gap < input.size) {
if (input[i] > input[i + gap]) {
val tmp = input[i]
input[i] = input[i + gap]
input[i + gap] = tmp
swaps = true
}
i++
}
}
}
fun main(args: Array<String>) {
val ia = arrayOf(28, 44, 46, 24, 19, 2, 17, 11, 25, 4)
println("Unsorted : ${ia.contentToString()}")
combSort(ia)
println("Sorted : ${ia.contentToString()}")
println()
val ca = arrayOf('X', 'B', 'E', 'A', 'Z', 'M', 'S', 'L', 'Y', 'C')
println("Unsorted : ${ca.contentToString()}")
combSort(ca)
println("Sorted : ${ca.contentToString()}")
}

View file

@ -0,0 +1,13 @@
fcn combSort(list){
len,gap,swaps:=list.len(),len,True;
while(gap>1 or swaps){
gap,swaps=(1).max(gap.toFloat()/1.2473), False;
foreach i in (len - gap){
if(list[i]>list[i + gap]){
list.swap(i,i + gap);
swaps=True;
}
}
}
list
}

View file

@ -0,0 +1,2 @@
combSort(List(28, 44, 46, 24, 19, 2, 17, 11, 25, 4)).println();
combSort("This is a test".toData()).text.println();