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