Sorting algorithms/Comb sort: Difference between revisions

→‎Tcl: Added implementation
(Added Oz.)
(→‎Tcl: Added implementation)
Line 152:
 
 
=={{header|Tcl}}==
<lang tcl>proc combsort {input} {
set gap [llength $input]
while 1 {
set gap [expr {int(floor($gap / 1.3))}]
set swaps 0
for {set i 0} {$i+$gap < [llength $input]} {incr i} {
set j [expr {$i+$gap}]
if {[lindex $input $i] > [lindex $input $j]} {
set tmp [lindex $input $i]
lset input $i [lindex $input $j]
lset input $j $tmp
incr swaps
}
}
if {$gap <= 1 && !$swaps} break
}
return $input
}
 
set data {23 76 99 58 97 57 35 89 51 38 95 92 24 46 31 24 14 12 57 78}
puts [combsort $data]</lang>
Produces this output:
<pre>12 14 23 24 24 31 35 38 46 51 57 57 58 76 78 89 92 95 97 99</pre>
=={{header|TI-83 BASIC}}==
Requires [[Insertion sort#TI-83 BASIC|prgmSORTINS]]. Gap division of 1.3. Switches to [[Insertion sort]] when gap is less than 5.
Anonymous user