Sorting algorithms/Comb sort: Difference between revisions
Added Easylang
(→{{header|ALGOL 68}}: fix psedo code translation) |
(Added Easylang) |
||
(7 intermediate revisions by 6 users not shown) | |||
Line 55:
{{trans|Python}}
<
V gap = input.len
V swaps = 1B
Line 70:
combsort(&y)
assert(y == sorted(y))
print(y)</
{{out}}
Line 80:
Translation from prototype.<br>
The program uses ASM structured macros and two ASSIST macros to keep the code as short as possible.
<
COMBSORT CSECT
USING COMBSORT,R13 base register
Line 147:
YREGS
RI EQU 6 i
END COMBSORT</
{{out}}
<pre>
Line 155:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program combSort64.s */
Line 333:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Action!}}==
<
INT i
Line 394:
Test(c,8)
Test(d,12)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Comb_sort.png Screenshot from Atari 8-bit computer]
Line 420:
=={{header|ActionScript}}==
<
{
var gap:uint = input.length;
Line 440:
}
return input;
}</
=={{header|Ada}}==
<
procedure Comb_Sort is
generic
Line 490:
end loop;
Ada.Text_IO.New_Line;
end Comb_Sort;</
Output:
Line 497:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-rows}}
<
PR read "rows.incl.a68" PR # include row (array) utilities - SHOW is used to display the array #
# comb-sorts in-place the array of integers input #
Line 534:
print( ( " -> " ) );
SHOW data
END</
{{out}}
<pre>
9 -4 0 2 3 77 1 -> -4 0 1 2 3 9 77
</pre>
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<syntaxhighlight lang="algolw">begin % comb sort %
% comb-sorts in-place the array of integers input with bounds lb :: ub %
procedure combSort ( integer array input ( * )
; integer value lb, ub
) ;
begin
integer inputSize, gap, i;
inputSize := ( ub - lb ) + 1;
if inputSize > 1 then begin
% more than one element, so must sort %
logical swapped;
gap := inputSize; % initial gap is the whole array size %
swapped := true;
while gap not = 1 or swapped do begin
% update the gap value for a next comb %
gap := entier( gap / 1.25 );
if gap < 1 then begin
% ensure the gap is at least 1 %
gap := 1
end if_gap_lt_1 ;
swapped := false;
% a single "comb" over the input list %
i := lb;
while i + gap <= ub do begin
integer t, iGap;
t := input( i );
iGap := i + gap;
if t > input( iGap ) then begin
% need to swap out-of-order items %
input( i ) := input( iGap );
input( iGap ) := t;
swapped := true % Flag a swap has occurred, so the list is not guaranteed sorted yet %
end if_t_gt_input__iGap ;
i := i + 1
end while_i_plus_gap_le_ub
end while_gap_ne_1_or_swapped
end if_inputSize_gt_1
end combSort ;
begin % test %
integer array data ( 1 :: 7 );
integer dPos;
dPos := 0;
for v := 9, -4, 0, 2, 3, 77, 1 do begin dPos := dPos + 1; data( dPos ) := v end;
for i := 1 until 7 do writeon( i_w := 1, s_w := 0, " ", data( i ) );
combSort( data, 1, 7 );
writeon( ( " -> " ) );
for i := 1 until 7 do writeon( i_w := 1, s_w := 0, " ", data( i ) )
end
end.</syntaxhighlight>
{{out}}
<pre>
Line 541 ⟶ 597:
=={{header|AppleScript}}==
<
-- Comb sort algorithm: Włodzimierz Dobosiewicz and Artur Borowy, 1980. Stephen Lacey and Richard Box, 1991.
Line 623 ⟶ 679:
set aList to {7, 56, 70, 22, 94, 42, 5, 25, 54, 90, 29, 65, 87, 27, 4, 5, 86, 8, 2, 30, 87, 12, 85, 86, 7}
combSort(aList, 1, -1) -- Sort items 1 thru -1 of aList.
aList</
{{output}}
<
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program combSort.s */
Line 793 ⟶ 849:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
=={{header|Arturo}}==
<
a: new items
gap: size a
Line 820 ⟶ 876:
]
print combSort [3 1 2 8 5 7 9 4 6]</
{{out}}
Line 827 ⟶ 883:
=={{header|AutoHotkey}}==
<
List2 = 88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70
Line 874 ⟶ 930:
List .= (A_Index = 1 ? "" : ",") %Array%%A_Index%
Return, List
}</
Message (1) box shows:
<pre>23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78
Line 883 ⟶ 939:
=={{header|AWK}}==
<
{
gap = len
Line 920 ⟶ 976:
for( i=0; i<length(a); i++ )
print a[i]
}</
=={{header|BBC BASIC}}==
<
gap%=Size%
Line 942 ⟶ 998:
UNTIL gap%=1 AND Finished%
ENDPROC</
=={{header|C}}==
Implementation of Combsort11. Its efficiency can be improved by just switching to Insertion sort when the gap size becomes less than 10.
<
{
int i, j, gap, swapped = 1;
Line 969 ⟶ 1,025:
}
}
}</
=={{header|C sharp|C#}}==
<
namespace CombSort
Line 1,009 ⟶ 1,065:
}
}
}</
=={{header|C++}}==
This is copied from [[wp:Comb sort|the Wikipedia article]].
<
void combsort ( ForwardIterator first, ForwardIterator last )
{
Line 1,036 ⟶ 1,092:
}
}
}</
=={{header|COBOL}}==
This excerpt contains just enough of the procedure division to show the workings. See the example for the bubble sort for a more complete program.
<
C-000.
DISPLAY "SORT STARTING".
Line 1,080 ⟶ 1,136:
F-999.
EXIT.</
=={{header|Common Lisp}}==
<
(defun comb-sort (input)
Line 1,098 ⟶ 1,154:
(setf swapped t))
while (or (> gap 1) swapped)
finally (return input)))</
=={{header|D}}==
{{trans|Python}}
<
void combSort(T)(T[] input) pure nothrow @safe @nogc {
Line 1,123 ⟶ 1,179:
data.combSort;
data.writeln;
}</
{{out}}
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>
Line 1,130 ⟶ 1,186:
{{libheader| System.Types}}
'''Adaptation of Pascal'''
<syntaxhighlight lang="delphi">
program Comb_sort;
Line 1,225 ⟶ 1,281:
Readln;
end.</
{{out}}
<pre>
Line 1,234 ⟶ 1,290:
[0 1 2 3 4 5 6 7 8 9]
</pre>
=={{header|EasyLang}}==
<syntaxhighlight>
proc combsort . d[] .
gap = len d[]
while gap > 1 or swaps = 1
gap = higher 1 (gap div 1.25)
swaps = 0
for i = 1 to len d[] - gap
j = i + gap
if d[i] > d[j]
swap d[i] d[j]
swaps = 1
.
.
.
.
d[] = [ 88 18 31 44 4 0 8 81 14 78 20 76 84 33 73 75 82 5 62 70 ]
combsort d[]
print d[]
</syntaxhighlight>
{{out}}
<pre>
[ 0 4 5 8 14 18 20 31 33 44 62 70 73 75 76 78 81 82 84 88 ]
</pre>
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
Line 1,308 ⟶ 1,389:
end
</syntaxhighlight>
Test:
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 1,343 ⟶ 1,424:
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,354 ⟶ 1,435:
=={{header|Elena}}==
ELENA 5.0 :
<
import system'math;
import system'routines;
Line 1,395 ⟶ 1,476:
console.printLine("before:", list.asEnumerable());
console.printLine("after :", list.combSort().asEnumerable())
}</
{{out}}
<pre>
Line 1,403 ⟶ 1,484:
=={{header|Elixir}}==
<
def comb_sort([]), do: []
def comb_sort(input) do
Line 1,423 ⟶ 1,504:
end
(for _ <- 1..20, do: :rand.uniform(20)) |> IO.inspect |> Sort.comb_sort |> IO.inspect</
{{out}}
Line 1,433 ⟶ 1,514:
=={{header|Forth}}==
This is an implementation of Comb sort with a different ending. Here [[Gnome sort]] is used, since it is rather small. The dataset is rather large, because otherwise the Comb sort routine would never kick in, passing control to Gnome sort almost right away. Note Comb sort can be kept much simpler this way, because Combsort11 optimizations and swapped flags can be discarded.
<
defer exchange
Line 1,479 ⟶ 1,560:
: .array 100 0 do example i cells + ? loop cr ;
.array example 100 combsort .array</
===Less Clever Version===
This version is an academic demonstration that aligns with the algorithm. As is, it is limited to use one static array and sorts in ascending order only.
<
HEX
\ gratuitous variables for clarity
Line 1,524 ⟶ 1,605:
UNTIL
DROP
;</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 1,569 ⟶ 1,650:
end subroutine combsort
end program Combsort_Demo</
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
' for boundary checks on array's compile with: fbc -s console -exx
Line 1,658 ⟶ 1,739:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>normal comb sort
Line 1,670 ⟶ 1,751:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=ade780ac2893fcfc95bf0d3feff6a3a8 Click this link to run this code]'''
<
Dim siToSort As Short[] = [249, 28, 111, 36, 171, 98, 29, 448, 44, 147, 154, 46, 102, 183, 24,
120, 19, 123, 2, 17, 226, 11, 211, 25, 191, 205, 77]
Line 1,712 ⟶ 1,793:
Print
End</
Output:
<pre>
Line 1,731 ⟶ 1,812:
=={{header|Go}}==
<
import "fmt"
Line 1,765 ⟶ 1,846:
}
}
}</
More generic version that sorts anything that implements <code>sort.Interface</code>:
<
import (
Line 1,805 ⟶ 1,886:
}
}
}</
=={{header|Groovy}}==
Combsort solution:
<
def checkSwap = { a, i, j -> [(a[i] > a[j])].find { it }.each { makeSwap(a, i, j) } }
Line 1,824 ⟶ 1,905:
}
input
}</
Combsort11 solution:
<
def swap = checkSwap.curry(input)
def size = input.size()
Line 1,838 ⟶ 1,919:
}
input
}</
Test:
<
println (combSort11([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4]))
println ()
println (combSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1]))
println (combSort11([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1]))</
Output:
Line 1,856 ⟶ 1,937:
=={{header|Haskell}}==
<
import Control.Arrow
import Control.Monad
Line 1,868 ⟶ 1,949:
combSort xs = (snd. fst) $ until (\((b,_),g)-> b && g==1)
(\((_,xs),g) ->(gapSwapping g xs, fg g)) ((False,xs), fg $ length xs)
where fg = max 1. truncate. (/1.25). fromIntegral</
Example:
<
[12,14,23,24,24,31,35,38,46,51,57,57,58,76,78,89,92,95,97,99]</
=={{header|Haxe}}==
<
@:generic
public static function sort<T>(arr:Array<T>) {
Line 1,916 ⟶ 1,997:
Sys.println('Sorted Strings: ' + stringArray);
}
}</
{{out}}
Line 1,929 ⟶ 2,010:
=={{header|Icon}} and {{header|Unicon}}==
<
demosort(combsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
Line 1,950 ⟶ 2,031:
}
return X
end</
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]]. The full demosort exercises the named sort of a list with op = "numeric", "string", ">>" (lexically gt, descending),">" (numerically gt, descending), a custom comparator, and also a string.
Line 1,962 ⟶ 2,043:
=={{header|Io}}==
<
combSortInPlace := method(
gap := size
Line 1,982 ⟶ 2,063:
lst := list(23, 76, 99, 58, 97, 57, 35, 89, 51, 38, 95, 92, 24, 46, 31, 24, 14, 12, 57, 78)
lst combSortInPlace println # ==> list(12, 14, 23, 24, 24, 31, 35, 38, 46, 51, 57, 57, 58, 76, 78, 89, 92, 95, 97, 99)</
=={{header|IS-BASIC}}==
<
110 RANDOMIZE
120 NUMERIC ARRAY(11 TO 30)
Line 2,014 ⟶ 2,095:
370 NEXT
380 LOOP
390 END DEF</
=={{header|J}}==
Line 2,020 ⟶ 2,101:
Large gap sizes allow some parallelism in comparisons and swaps. (If the gap size is G, then G pairs can be compared and swapped in parallel.) Beyond that, however, the data flow complexity of this algorithm requires a fair bit of micro-management.
<
gap=. #y
whilst.1 < gap+swaps do.
Line 2,032 ⟶ 2,113:
end.
y
)</
Example use:
Line 2,042 ⟶ 2,123:
=={{header|Java}}==
This is copied from [[wp:Comb sort|the Wikipedia article]].
<
int gap = input.length;
boolean swapped = true;
Line 2,059 ⟶ 2,140:
}
}
}</
=={{header|JavaScript}}==
<
// Node 5.4.1 tested implementation (ES6)
function is_array_sorted(arr) {
Line 2,109 ⟶ 2,190:
// Print the sorted array
console.log(arr);
}</
Line 2,120 ⟶ 2,201:
{{works with|jq|1.4}}
An implementation of the pseudo-code in the task description:
<
def combsort:
Line 2,153 ⟶ 2,234:
end)
| .[0] = $gap )
| .[2] ;</
=={{header|Julia}}==
<
function combsort!(x::Array)::Array
Line 2,176 ⟶ 2,257:
x = randn(100)
@show x combsort!(x)
@assert issorted(x)</
{{out}}
Line 2,183 ⟶ 2,264:
=={{header|Kotlin}}==
<
fun <T : Comparable<T>> combSort(input: Array<T>) {
Line 2,216 ⟶ 2,297:
combSort(ca)
println("Sorted : ${ca.contentToString()}")
}</
{{out}}
Line 2,228 ⟶ 2,309:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
'randomize 0.5
itemCount = 20
Line 2,267 ⟶ 2,348:
next i
end
</syntaxhighlight>
=={{header|Lua}}==
<
local gapd, gap, swaps = 1.2473, #t, 0
while gap + swaps > 1 do
Line 2,285 ⟶ 2,366:
end
print(unpack(combsort{3,5,1,2,7,4,8,3,6,4,1}))</
=={{header|Maple}}==
<
local temp;
temp := arr[a]:
Line 2,319 ⟶ 2,400:
arr := Array([17,3,72,0,36,2,3,8,40,0]);
combsort(arr, numelems(arr));
arr;</
{{Out|Output}}
<pre>[0,0,2,3,3,8,17,36,40,72]</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
gap = listSize = Length[list];
While[ !((gap <= 1) && (swaps == False)),
Line 2,338 ⟶ 2,419:
]
]
]</
<pre>combSort@{2, 1, 3, 7, 6}
->{1, 2, 3, 6, 7}</pre>
=={{header|MATLAB}} / {{header|Octave}}==
<
listSize = numel(list);
Line 2,372 ⟶ 2,453:
end %while
end %while
end %combSort</
Sample Output:
<
ans =
1 2 3 4 5 6</
=={{header|MAXScript}}==
<
(
local gap = arr.count
Line 2,409 ⟶ 2,490:
)
return arr
)</
Output:
<syntaxhighlight lang="maxscript">
a = for i in 1 to 10 collect random 1 10
#(2, 6, 5, 9, 10, 7, 2, 6, 1, 4)
combsort a
#(1, 2, 2, 4, 5, 6, 6, 7, 9, 10)
</syntaxhighlight>
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols binary
Line 2,470 ⟶ 2,551:
method isFalse public constant binary returns boolean
return \isTrue
</syntaxhighlight>
;Output
<pre>
Line 2,493 ⟶ 2,574:
=={{header|Nim}}==
<
var gap = a.len
var swapped = true
Line 2,510 ⟶ 2,591:
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
combSort a
echo a</
Output:
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre>
=={{header|Objeck}}==
<
bundle Default {
class Stooge {
Line 2,549 ⟶ 2,630:
}
}
</syntaxhighlight>
=={{header|OCaml}}==
<
let input_length = Array.length input in
let gap = ref(input_length) in
Line 2,571 ⟶ 2,652:
done
done
;;</
=={{header|Oz}}==
<
proc {CombSort Arr}
Low = {Array.low Arr}
Line 2,601 ⟶ 2,682:
in
{CombSort Arr}
{Show {Array.toRecord unit Arr}}</
=={{header|PARI/GP}}==
<
my(phi=(1+sqrt(5))/2,magic=1/(1-exp(-phi)),g=#v,swaps);
while(g>1 | swaps,
Line 2,619 ⟶ 2,700:
);
v
};</
=={{header|Pascal}}==
<
Line 2,674 ⟶ 2,755:
end;
writeln;
end.</
Output:
<pre>
Line 2,683 ⟶ 2,764:
</pre>
<
Line 2,735 ⟶ 2,816:
end;
writeln;
end.</
=={{header|Perl}}==
<
my @arr = @_;
my $gap = @arr;
Line 2,753 ⟶ 2,834:
}
return @arr;
}</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 2,778 ⟶ 2,859:
<span style="color: #0000FF;">?</span><span style="color: #000000;">comb_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)))</span>
<!--</
{{out}}
<pre>
Line 2,785 ⟶ 2,866:
=={{header|PHP}}==
<
$gap = count($arr);
$swap = true;
Line 2,802 ⟶ 2,883:
}
return $arr;
}</
=={{header|PicoLisp}}==
<
(let (Gap (length Lst) Swaps NIL)
(while (or (> Gap 1) Swaps)
Line 2,816 ⟶ 2,897:
(on Swaps) )
(pop 'Lst) ) ) ) )
Lst )</
Output:
<pre>: (combSort (88 18 31 44 4 0 8 81 14 78 20 76 84 33 73 75 82 5 62 70))
Line 2,822 ⟶ 2,903:
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
/* From the pseudocode. */
comb_sort: procedure (A);
Line 2,848 ⟶ 2,929:
end;
end comb_sort;
</syntaxhighlight>
=={{header|PowerShell}}==
Massaging gap to always hit 11. Based on PowerShell from [[Cocktail Sort]]
<
$l = $a.Length
$gap = 11
Line 2,882 ⟶ 2,963:
}
$l = 100; CombSort ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( -( $l - 1 ), $l - 1 ) } )</
=={{header|PureBasic}}==
Implementation of CombSort11.
<
Procedure combSort11(Array a(1))
Protected i, gap, swaps = 1
Line 2,907 ⟶ 2,988:
Wend
Wend
EndProcedure</
Implementation of CombSort.
<
Procedure combSort(Array a(1))
Protected i, gap, swaps = 1
Line 2,928 ⟶ 3,009:
Wend
Wend
EndProcedure</
=={{header|Python}}==
<
gap = len(input)
swaps = True
Line 2,949 ⟶ 3,030:
>>> y
[0, 4, 5, 8, 14, 18, 20, 31, 33, 44, 62, 70, 73, 75, 76, 78, 81, 82, 84, 88]
>>> </
=={{header|R}}==
<syntaxhighlight lang="r">
comb.sort<-function(a){
gap<-length(a)
Line 2,974 ⟶ 3,055:
}
</syntaxhighlight>
=={{header|Racket}}==
<
#lang racket
(require (only-in srfi/43 vector-swap!))
Line 2,996 ⟶ 3,077:
[swaps])))))
xs)
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku"
my $gap = +@a;
my $swaps = 1;
Line 3,021 ⟶ 3,102:
my @weights = (^50).map: { 100 + ( 1000.rand.Int / 10 ) };
say @weights.sort.Str eq @weights.&comb_sort.Str ?? 'ok' !! 'not ok';
</syntaxhighlight>
=={{header|REXX}}==
<
call gen /*generate the @ array elements. */
call show 'before sort' /*display the before array elements. */
Line 3,058 ⟶ 3,139:
#= #-1; w= length(#); return /*adjust # because of DO loop.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do k=1 for #; say right('element',15) right(k,w) arg(1)":" @.k; end; return</
Data trivia: A ''hendecagon'' (also known as an ''undecagon'' or ''unidecagon'') is
Line 3,117 ⟶ 3,198:
=={{header|Ring}}==
<
aList = [3,5,1,2,7,4,8,3,6,4,1]
see combsort(aList)
Line 3,138 ⟶ 3,219:
end
return t
</syntaxhighlight>
=={{header|Ruby}}==
<
def combsort!
gap = size
Line 3,159 ⟶ 3,240:
end
p [23, 76, 99, 58, 97, 57, 35, 89, 51, 38, 95, 92, 24, 46, 31, 24, 14, 12, 57, 78].combsort!</
results in
<pre>[12, 14, 23, 24, 24, 31, 35, 38, 46, 51, 57, 57, 58, 76, 78, 89, 92, 95, 97, 99]</pre>
=={{header|Rust}}==
<
let len = a.len();
let mut gap = len;
Line 3,190 ⟶ 3,271:
comb_sort(&mut v);
println!("after: {:?}", v);
}</
{{out}}
Line 3,199 ⟶ 3,280:
=={{header|Sather}}==
<
private swap(inout a, inout b:T) is
Line 3,235 ⟶ 3,316:
#OUT + b + "\n";
end;
end;</
=={{header|Scala}}==
===Imperative version (Ugly, side effects)===
<
val ia = Array(28, 44, 46, 24, 19, 2, 17, 11, 25, 4)
val ca = Array('X', 'B', 'E', 'A', 'Z', 'M', 'S', 'L', 'Y', 'C')
Line 3,267 ⟶ 3,348:
println(s"Sorted : ${sorted(ca).mkString("[", ", ", "]")}")
}</
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/7ykMPZx/0 ScalaFiddle (JavaScript)] or by [https://scastie.scala-lang.org/Gp1ZcxnPQAKvToWFZLU7OA Scastie (JVM)].
=={{header|Sidef}}==
<
var gap = arr.len;
var swaps = true;
Line 3,285 ⟶ 3,366:
}
return arr;
}</
=={{header|Swift}}==
{{trans|C}}
<
var swapped = true
var gap = list.count
Line 3,311 ⟶ 3,392:
}
}
}</
=={{header|Tcl}}==
<
set gap [llength $input]
while 1 {
Line 3,334 ⟶ 3,415:
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]</
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>
Line 3,370 ⟶ 3,451:
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">PRINT "Comb sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
Line 3,428 ⟶ 3,509:
PRINT
RETURN</
=={{header|VBA}}==
{[trans|Phix}}<
Dim gap As Integer: gap = UBound(s)
Dim swapped As Integer
Line 3,457 ⟶ 3,538:
Debug.Print Join(s, ", ")
Debug.Print Join(comb_sort(s), ", ")
End Sub</
<pre>45, 414, 862, 790, 373, 961, 871, 56, 949, 364
45, 56, 364, 373, 414, 790, 862, 871, 949, 961</pre>
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn main() {
mut a := [170, 45, 75, -90, -802, 24, 2, 66]
println("before: $a")
comb_sort(mut a)
println("after: $a")
}
fn comb_sort(mut a []int) {
if a.len < 2 {
return
}
for gap := a.len; ; {
if gap > 1 {
gap = gap * 4 / 5
}
mut swapped := false
for i := 0; ; {
if a[i] > a[i+gap] {
a[i], a[i+gap] = a[i+gap], a[i]
swapped = true
}
i++
if i+gap >= a.len {
break
}
}
if gap == 1 && !swapped {
break
}
}
}</syntaxhighlight>
=={{header|Wren}}==
<
var gap = a.count
while (true) {
Line 3,483 ⟶ 3,598:
}
var
for (a in
System.print("Before: %(a)")
combSort.call(a)
System.print("After : %(a)")
System.print()
}</
{{out}}
Line 3,499 ⟶ 3,614:
After : [1, 2, 2, 3, 4, 5, 6, 6, 7]
</pre>
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0">
\Comb sorts in-place the array of integers Input with bounds LB :: UB
procedure CombSort ( Input, LB, UB );
integer Input, LB, UB;
integer InputSize, Gap, I, Swapped, T, IGap;
begin
InputSize := ( UB - LB ) + 1;
if InputSize > 1 then begin
\more than one element, so must sort
Gap := InputSize; \initial Gap is the whole array size
Swapped := true;
while Gap # 1 or Swapped do begin
\update the Gap value for a next comb
Gap := fix( Floor(float(Gap) / 1.25) );
if Gap < 1 then begin
\ensure the Gap is at least 1
Gap := 1
end; \if_Gap_lt_1
Swapped := false;
\a single "comb" over the input list
I := LB;
while I + Gap <= UB do begin
T := Input( I );
IGap := I + Gap;
if T > Input( IGap ) then begin
\need to swap out-of-order items
Input( I ) := Input( IGap );
Input( IGap ) := T;
\Flag a swap has occurred, so the list is not guaranteed sorted yet
Swapped := true
end; \if_t_gt_input__iGap
I := I + 1
end \while_I_plus_Gap_le_UB
end \while_Gap_ne_1_or_swapped
end \if_inputSize_gt_1
end; \combSort
integer Data, I;
begin \test
Data:= [0, 9, -4, 0, 2, 3, 77, 1];
for I := 1 to 7 do begin Text(0, " "); IntOut(0, Data( I ) ) end;
CombSort( Data, 1, 7 );
Text(0, ( " -> " ) );
for I := 1 to 7 do begin Text(0, " "); IntOut(0, Data( I ) ) end;
end</syntaxhighlight>
{{out}}
<pre>
9 -4 0 2 3 77 1 -> -4 0 1 2 3 9 77</pre>
=={{header|zkl}}==
{{trans|D}}
<
len,gap,swaps:=list.len(),len,True;
while(gap>1 or swaps){
Line 3,514 ⟶ 3,680:
}
list
}</
<
combSort("This is a test".toData()).text.println();</
{{out}}
<pre>
|