Sorting algorithms/Comb sort: Difference between revisions

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 55:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F combsort(&input)
V gap = input.len
V swaps = 1B
Line 70:
combsort(&y)
assert(y == sorted(y))
print(y)</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="360asm">* Comb sort 23/06/2016
COMBSORT CSECT
USING COMBSORT,R13 base register
Line 147:
YREGS
RI EQU 6 i
END COMBSORT</langsyntaxhighlight>
{{out}}
<pre>
Line 155:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<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>
</lang>
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Line 394:
Test(c,8)
Test(d,12)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Comb_sort.png Screenshot from Atari 8-bit computer]
Line 420:
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">function combSort(input:Array)
{
var gap:uint = input.length;
Line 440:
}
return input;
}</langsyntaxhighlight>
 
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
procedure Comb_Sort is
generic
Line 490:
end loop;
Ada.Text_IO.New_Line;
end Comb_Sort;</langsyntaxhighlight>
 
Output:
Line 497:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-rows}}
<langsyntaxhighlight lang="algol68">BEGIN # comb sort #
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</langsyntaxhighlight>
{{out}}
<pre>
Line 542:
=={{header|ALGOL W}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="algolw">begin % comb sort %
% comb-sorts in-place the array of integers input with bounds lb :: ub %
procedure combSort ( integer array input ( * )
Line 590:
for i := 1 until 7 do writeon( i_w := 1, s_w := 0, " ", data( i ) )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 597:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">-- Comb sort with insertion sort finish.
-- Comb sort algorithm: Włodzimierz Dobosiewicz and Artur Borowy, 1980. Stephen Lacey and Richard Box, 1991.
 
Line 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</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{2, 4, 5, 5, 7, 7, 8, 12, 22, 25, 27, 29, 30, 42, 54, 56, 65, 70, 85, 86, 86, 87, 87, 90, 94}</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program combSort.s */
Line 849:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">combSort: function [items][
a: new items
gap: size a
Line 876:
]
 
print combSort [3 1 2 8 5 7 9 4 6]</langsyntaxhighlight>
 
{{out}}
Line 883:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">List1 = 23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78
List2 = 88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70
 
Line 930:
List .= (A_Index = 1 ? "" : ",") %Array%%A_Index%
Return, List
}</langsyntaxhighlight>
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 939:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">function combsort( a, len, gap, igap, swap, swaps, i )
{
gap = len
Line 976:
for( i=0; i<length(a); i++ )
print a[i]
}</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight BBClang="bbc BASICbasic">DEF PROC_CombSort11(Size%)
 
gap%=Size%
Line 998:
UNTIL gap%=1 AND Finished%
 
ENDPROC</langsyntaxhighlight>
 
=={{header|C}}==
Implementation of Combsort11. Its efficiency can be improved by just switching to Insertion sort when the gap size becomes less than 10.
<langsyntaxhighlight lang="c">void Combsort11(double a[], int nElements)
{
int i, j, gap, swapped = 1;
Line 1,025:
}
}
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace CombSort
Line 1,065:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
This is copied from [[wp:Comb sort|the Wikipedia article]].
<langsyntaxhighlight lang="cpp">template<class ForwardIterator>
void combsort ( ForwardIterator first, ForwardIterator last )
{
Line 1,092:
}
}
}</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight COBOLlang="cobol"> C-PROCESS SECTION.
C-000.
DISPLAY "SORT STARTING".
Line 1,136:
 
F-999.
EXIT.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defparameter *shrink* 1.3)
 
(defun comb-sort (input)
Line 1,154:
(setf swapped t))
while (or (> gap 1) swapped)
finally (return input)))</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm;
 
void combSort(T)(T[] input) pure nothrow @safe @nogc {
Line 1,179:
data.combSort;
data.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>
Line 1,186:
{{libheader| System.Types}}
'''Adaptation of Pascal'''
<syntaxhighlight lang="delphi">
<lang Delphi>
program Comb_sort;
 
Line 1,281:
 
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,291:
</pre>
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
 
class
Line 1,364:
end
 
</syntaxhighlight>
</lang>
Test:
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,399:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,410:
=={{header|Elena}}==
ELENA 5.0 :
<langsyntaxhighlight lang="elena">import extensions;
import system'math;
import system'routines;
Line 1,451:
console.printLine("before:", list.asEnumerable());
console.printLine("after :", list.combSort().asEnumerable())
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,459:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Sort do
def comb_sort([]), do: []
def comb_sort(input) do
Line 1,479:
end
 
(for _ <- 1..20, do: :rand.uniform(20)) |> IO.inspect |> Sort.comb_sort |> IO.inspect</langsyntaxhighlight>
 
{{out}}
Line 1,489:
=={{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.
<langsyntaxhighlight lang="forth">defer precedes
defer exchange
Line 1,535:
: .array 100 0 do example i cells + ? loop cr ;
.array example 100 combsort .array</langsyntaxhighlight>
 
===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.
<langsyntaxhighlight lang="forth">\ combsort for the Forth Newbie (GForth)
HEX
\ gratuitous variables for clarity
Line 1,580:
UNTIL
DROP
;</LANGsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program Combsort_Demo
implicit none
Line 1,625:
end subroutine combsort
end program Combsort_Demo</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight Freebasiclang="freebasic">' version 21-10-2016
' compile with: fbc -s console
' for boundary checks on array's compile with: fbc -s console -exx
Line 1,714:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>normal comb sort
Line 1,726:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=ade780ac2893fcfc95bf0d3feff6a3a8 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
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,768:
Print
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,787:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,821:
}
}
}</langsyntaxhighlight>
 
More generic version that sorts anything that implements <code>sort.Interface</code>:
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,861:
}
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Combsort solution:
<langsyntaxhighlight lang="groovy">def makeSwap = { a, i, j -> print "."; a[i] ^= a[j]; a[j] ^= a[i]; a[i] ^= a[j] }
 
def checkSwap = { a, i, j -> [(a[i] > a[j])].find { it }.each { makeSwap(a, i, j) } }
Line 1,880:
}
input
}</langsyntaxhighlight>
 
Combsort11 solution:
<langsyntaxhighlight lang="groovy">def combSort11 = { input ->
def swap = checkSwap.curry(input)
def size = input.size()
Line 1,894:
}
input
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">println (combSort([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4]))
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]))</langsyntaxhighlight>
 
Output:
Line 1,912:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List
import Control.Arrow
import Control.Monad
Line 1,924:
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</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="haskell">*Main> combSort [23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78]
[12,14,23,24,24,31,35,38,46,51,57,57,58,76,78,89,92,95,97,99]</langsyntaxhighlight>
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">class CombSort {
@:generic
public static function sort<T>(arr:Array<T>) {
Line 1,972:
Sys.println('Sorted Strings: ' + stringArray);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,985:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main() #: demonstrate various ways to sort a list and string
demosort(combsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
Line 2,006:
}
return X
end</langsyntaxhighlight>
 
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 2,018:
 
=={{header|Io}}==
<langsyntaxhighlight lang="io">List do(
combSortInPlace := method(
gap := size
Line 2,038:
 
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)</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "CombSrt.bas"
110 RANDOMIZE
120 NUMERIC ARRAY(11 TO 30)
Line 2,070:
370 NEXT
380 LOOP
390 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
Line 2,076:
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.
 
<langsyntaxhighlight Jlang="j">combSort=:3 :0
gap=. #y
whilst.1 < gap+swaps do.
Line 2,088:
end.
y
)</langsyntaxhighlight>
 
Example use:
Line 2,098:
=={{header|Java}}==
This is copied from [[wp:Comb sort|the Wikipedia article]].
<langsyntaxhighlight lang="java">public static <E extends Comparable<? super E>> void sort(E[] input) {
int gap = input.length;
boolean swapped = true;
Line 2,115:
}
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">
// Node 5.4.1 tested implementation (ES6)
function is_array_sorted(arr) {
Line 2,165:
// Print the sorted array
console.log(arr);
}</langsyntaxhighlight>
 
 
Line 2,176:
{{works with|jq|1.4}}
An implementation of the pseudo-code in the task description:
<langsyntaxhighlight lang="jq"># Input should be the array to be sorted.
def combsort:
 
Line 2,209:
end)
| .[0] = $gap )
| .[2] ;</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># v0.6
 
function combsort!(x::Array)::Array
Line 2,232:
x = randn(100)
@show x combsort!(x)
@assert issorted(x)</langsyntaxhighlight>
 
{{out}}
Line 2,239:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun <T : Comparable<T>> combSort(input: Array<T>) {
Line 2,272:
combSort(ca)
println("Sorted : ${ca.contentToString()}")
}</langsyntaxhighlight>
 
{{out}}
Line 2,284:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
'randomize 0.5
itemCount = 20
Line 2,323:
next i
end
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function combsort(t)
local gapd, gap, swaps = 1.2473, #t, 0
while gap + swaps > 1 do
Line 2,341:
end
 
print(unpack(combsort{3,5,1,2,7,4,8,3,6,4,1}))</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">swap := proc(arr, a, b)
local temp;
temp := arr[a]:
Line 2,375:
arr := Array([17,3,72,0,36,2,3,8,40,0]);
combsort(arr, numelems(arr));
arr;</langsyntaxhighlight>
{{Out|Output}}
<pre>[0,0,2,3,3,8,17,36,40,72]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">combSort[list_] := Module[{ gap = 0, listSize = 0, swaps = True},
gap = listSize = Length[list];
While[ !((gap <= 1) && (swaps == False)),
Line 2,394:
]
]
]</langsyntaxhighlight>
<pre>combSort@{2, 1, 3, 7, 6}
->{1, 2, 3, 6, 7}</pre>
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">function list = combSort(list)
listSize = numel(list);
Line 2,428:
end %while
end %while
end %combSort</langsyntaxhighlight>
 
Sample Output:
<langsyntaxhighlight MATLABlang="matlab">>> combSort([4 3 1 5 6 2])
 
ans =
 
1 2 3 4 5 6</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight MAXScriptlang="maxscript">fn combSort arr =
(
local gap = arr.count
Line 2,465:
)
return arr
)</langsyntaxhighlight>
Output:
<syntaxhighlight lang="maxscript">
<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>
</lang>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
 
options replace format comments java crossref savelog symbols binary
Line 2,526:
method isFalse public constant binary returns boolean
return \isTrue
</syntaxhighlight>
</lang>
;Output
<pre>
Line 2,549:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">proc combSort[T](a: var openarray[T]) =
var gap = a.len
var swapped = true
Line 2,566:
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
combSort a
echo a</langsyntaxhighlight>
Output:
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
bundle Default {
class Stooge {
Line 2,605:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let comb_sort ~input =
let input_length = Array.length input in
let gap = ref(input_length) in
Line 2,627:
done
done
;;</langsyntaxhighlight>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
proc {CombSort Arr}
Low = {Array.low Arr}
Line 2,657:
in
{CombSort Arr}
{Show {Array.toRecord unit Arr}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">combSort(v)={
my(phi=(1+sqrt(5))/2,magic=1/(1-exp(-phi)),g=#v,swaps);
while(g>1 | swaps,
Line 2,675:
);
v
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program CombSortDemo;
 
 
Line 2,730:
end;
writeln;
end.</langsyntaxhighlight>
Output:
<pre>
Line 2,739:
</pre>
 
<langsyntaxhighlight lang="pascal">program CombSortDemo;
 
 
Line 2,791:
end;
writeln;
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub combSort {
my @arr = @_;
my $gap = @arr;
Line 2,809:
}
return @arr;
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 2,834:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,841:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">function combSort($arr){
$gap = count($arr);
$swap = true;
Line 2,858:
}
return $arr;
}</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de combSort (Lst)
(let (Gap (length Lst) Swaps NIL)
(while (or (> Gap 1) Swaps)
Line 2,872:
(on Swaps) )
(pop 'Lst) ) ) ) )
Lst )</langsyntaxhighlight>
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,878:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* From the pseudocode. */
comb_sort: procedure (A);
Line 2,904:
end;
end comb_sort;
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
Massaging gap to always hit 11. Based on PowerShell from [[Cocktail Sort]]
<langsyntaxhighlight PowerShelllang="powershell">function CombSort ($a) {
$l = $a.Length
$gap = 11
Line 2,938:
}
 
$l = 100; CombSort ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( -( $l - 1 ), $l - 1 ) } )</langsyntaxhighlight>
 
=={{header|PureBasic}}==
Implementation of CombSort11.
<langsyntaxhighlight PureBasiclang="purebasic">;sorts an array of integers
Procedure combSort11(Array a(1))
Protected i, gap, swaps = 1
Line 2,963:
Wend
Wend
EndProcedure</langsyntaxhighlight>
Implementation of CombSort.
<langsyntaxhighlight PureBasiclang="purebasic">;sorts an array of integers
Procedure combSort(Array a(1))
Protected i, gap, swaps = 1
Line 2,984:
Wend
Wend
EndProcedure</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> def combsort(input):
gap = len(input)
swaps = True
Line 3,005:
>>> y
[0, 4, 5, 8, 14, 18, 20, 31, 33, 44, 62, 70, 73, 75, 76, 78, 81, 82, 84, 88]
>>> </langsyntaxhighlight>
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
comb.sort<-function(a){
gap<-length(a)
Line 3,030:
}
 
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require (only-in srfi/43 vector-swap!))
Line 3,052:
[swaps])))))
xs)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>sub comb_sort ( @a is copy ) {
my $gap = +@a;
my $swaps = 1;
Line 3,077:
my @weights = (^50).map: { 100 + ( 1000.rand.Int / 10 ) };
say @weights.sort.Str eq @weights.&comb_sort.Str ?? 'ok' !! 'not ok';
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program sorts and displays a stemmed array using the comb sort algorithm. */
call gen /*generate the @ array elements. */
call show 'before sort' /*display the before array elements. */
Line 3,114:
#= #-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</langsyntaxhighlight>
 
Data trivia: &nbsp; A &nbsp; ''hendecagon'' &nbsp; (also known as an &nbsp; ''undecagon'' &nbsp; or &nbsp; ''unidecagon'') &nbsp; is
Line 3,173:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
aList = [3,5,1,2,7,4,8,3,6,4,1]
see combsort(aList)
Line 3,194:
end
return t
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Array
def combsort!
gap = size
Line 3,215:
end
 
p [23, 76, 99, 58, 97, 57, 35, 89, 51, 38, 95, 92, 24, 46, 31, 24, 14, 12, 57, 78].combsort!</langsyntaxhighlight>
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}}==
<langsyntaxhighlight lang="rust">fn comb_sort<T: PartialOrd>(a: &mut [T]) {
let len = a.len();
let mut gap = len;
Line 3,246:
comb_sort(&mut v);
println!("after: {:?}", v);
}</langsyntaxhighlight>
 
{{out}}
Line 3,255:
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class SORT{T < $IS_LT{T}} is
 
private swap(inout a, inout b:T) is
Line 3,291:
#OUT + b + "\n";
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
===Imperative version (Ugly, side effects)===
<langsyntaxhighlight Scalalang="scala">object CombSort extends App {
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,323:
println(s"Sorted : ${sorted(ca).mkString("[", ", ", "]")}")
 
}</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="ruby">func comb_sort(arr) {
var gap = arr.len;
var swaps = true;
Line 3,341:
}
return arr;
}</langsyntaxhighlight>
 
=={{header|Swift}}==
{{trans|C}}
<langsyntaxhighlight Swiftlang="swift">func combSort(inout list:[Int]) {
var swapped = true
var gap = list.count
Line 3,367:
}
}
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc combsort {input} {
set gap [llength $input]
while 1 {
Line 3,390:
 
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]</langsyntaxhighlight>
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,426:
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">PRINT "Comb sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
Line 3,484:
PRINT
RETURN</langsyntaxhighlight>
 
=={{header|VBA}}==
{[trans|Phix}}<langsyntaxhighlight lang="vb">Function comb_sort(ByVal s As Variant) As Variant
Dim gap As Integer: gap = UBound(s)
Dim swapped As Integer
Line 3,513:
Debug.Print Join(s, ", ")
Debug.Print Join(comb_sort(s), ", ")
End Sub</langsyntaxhighlight>{{out}}
<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")
Line 3,549:
}
}
}</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var combSort = Fn.new { |a|
var gap = a.count
while (true) {
Line 3,573:
}
 
var asarray = [ [4, 65, 2, -31, 0, 99, 2, 83, 782, 1], [7, 5, 2, 6, 1, 4, 2, 6, 3] ]
for (a in asarray) {
System.print("Before: %(a)")
combSort.call(a)
System.print("After : %(a)")
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 3,589:
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}}
<langsyntaxhighlight lang="zkl">fcn combSort(list){
len,gap,swaps:=list.len(),len,True;
while(gap>1 or swaps){
Line 3,604 ⟶ 3,655:
}
list
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">combSort(List(28, 44, 46, 24, 19, 2, 17, 11, 25, 4)).println();
combSort("This is a test".toData()).text.println();</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits