Below is the basic shell sort algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | proc shellsort args {
set key 0
set increment 3
foreach arg $args {
set numbers($key) $arg
incr key
}
set number [array size numbers]
while {$increment > 0} {
for {set i 0} {$i < $number} {incr i} {
set j $i
set temp $numbers($i)
while {($j >=$increment) && ($numbers([expr $j-$increment]) > $temp)} {
set numbers($j) $numbers([expr $j - $increment])
set j [expr $j - $increment]
}
set numbers($j) $temp
}
if {[expr $increment/2] != 0} {
set increment [expr $increment/2]
} elseif {$increment == 1} {
set increment 0
} else {
set increment 1
}
}
for {set i 0} {$i < $number} {incr i} {
lappend sort $numbers($i)
}
puts $sort
}
# Put the numbers after command separated with spaces
shellsort
|