Sorting algorithms/Gnome sort: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 34: | Line 34: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F gnomesort(&a) |
||
V i = 1 |
V i = 1 |
||
V j = 2 |
V j = 2 |
||
Line 49: | Line 49: | ||
R a |
R a |
||
print(gnomesort(&[3, 4, 2, 5, 1, 6]))</ |
print(gnomesort(&[3, 4, 2, 5, 1, 6]))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 57: | Line 57: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
< |
<syntaxhighlight lang="360asm">* Gnome sort - Array base 0 - 15/04/2020 |
||
GNOME CSECT |
GNOME CSECT |
||
USING GNOME,R13 base register |
USING GNOME,R13 base register |
||
Line 112: | Line 112: | ||
PG DC CL125' ' buffer |
PG DC CL125' ' buffer |
||
REGEQU |
REGEQU |
||
END GNOME</ |
END GNOME</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 120: | Line 120: | ||
=={{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 gnomeSort64.s */ |
/* program gnomeSort64.s */ |
||
Line 286: | Line 286: | ||
.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 342: | Line 342: | ||
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/Gnome_sort.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Gnome_sort.png Screenshot from Atari 8-bit computer] |
||
Line 368: | Line 368: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">function gnomeSort(array:Array) |
||
{ |
{ |
||
var pos:uint = 0; |
var pos:uint = 0; |
||
Line 384: | Line 384: | ||
} |
} |
||
return array; |
return array; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
This example is a generic procedure for constrained array types. |
This example is a generic procedure for constrained array types. |
||
< |
<syntaxhighlight lang="ada">generic |
||
type Element_Type is private; |
type Element_Type is private; |
||
type Index is (<>); |
type Index is (<>); |
||
type Collection is array(Index) of Element_Type; |
type Collection is array(Index) of Element_Type; |
||
with function "<=" (Left, Right : Element_Type) return Boolean is <>; |
with function "<=" (Left, Right : Element_Type) return Boolean is <>; |
||
procedure Gnome_Sort(Item : in out Collection);</ |
procedure Gnome_Sort(Item : in out Collection);</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ada">procedure Gnome_Sort(Item : in out Collection) is |
||
procedure Swap(Left, Right : in out Element_Type) is |
procedure Swap(Left, Right : in out Element_Type) is |
||
Temp : Element_Type := Left; |
Temp : Element_Type := Left; |
||
Line 419: | Line 419: | ||
end if; |
end if; |
||
end loop; |
end loop; |
||
end Gnome_Sort;</ |
end Gnome_Sort;</syntaxhighlight> |
||
Usage example: |
Usage example: |
||
< |
<syntaxhighlight lang="ada">with Gnome_Sort; |
||
with Ada.Text_Io; use Ada.Text_Io; |
with Ada.Text_Io; use Ada.Text_Io; |
||
Line 439: | Line 439: | ||
end loop; |
end loop; |
||
New_Line; |
New_Line; |
||
end Gnome_Sort_Test;</ |
end Gnome_Sort_Test;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 447: | Line 447: | ||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards AND formatted transput statements removed) - tested with release 1.8.8d.fc9.i386}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards AND formatted transput statements removed) - tested with release 1.8.8d.fc9.i386}} |
||
< |
<syntaxhighlight lang="algol68">MODE SORTSTRUCT = CHAR; |
||
PROC inplace gnome sort = (REF[]SORTSTRUCT list)REF[]SORTSTRUCT: |
PROC inplace gnome sort = (REF[]SORTSTRUCT list)REF[]SORTSTRUCT: |
||
Line 468: | Line 468: | ||
[]SORTSTRUCT char array data = "big fjords vex quick waltz nymph"; |
[]SORTSTRUCT char array data = "big fjords vex quick waltz nymph"; |
||
print((gnome sort(char array data), new line))</ |
print((gnome sort(char array data), new line))</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 475: | Line 475: | ||
=={{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 gnomeSort.s */ |
/* program gnomeSort.s */ |
||
Line 632: | Line 632: | ||
.include "../affichage.inc" |
.include "../affichage.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">gnomeSort: function [items][ |
||
i: 1 |
i: 1 |
||
j: 2 |
j: 2 |
||
Line 660: | Line 660: | ||
] |
] |
||
print gnomeSort [3 1 2 8 5 7 9 4 6]</ |
print gnomeSort [3 1 2 8 5 7 9 4 6]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 668: | Line 668: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276379.html#276379 forum] |
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276379.html#276379 forum] |
||
< |
<syntaxhighlight lang="autohotkey">MsgBox % GnomeSort("") |
||
MsgBox % GnomeSort("xxx") |
MsgBox % GnomeSort("xxx") |
||
MsgBox % GnomeSort("3,2,1") |
MsgBox % GnomeSort("3,2,1") |
||
Line 689: | Line 689: | ||
sorted .= "," . a%A_Index% |
sorted .= "," . a%A_Index% |
||
Return SubStr(sorted,2) ; drop leading comma |
Return SubStr(sorted,2) ; drop leading comma |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 697: | Line 697: | ||
This version includes the mark/resume optimization. It remembers where it was before backing up so that once an item is backed up to its proper place the process resumes from where it was before backing up. |
This version includes the mark/resume optimization. It remembers where it was before backing up so that once an item is backed up to its proper place the process resumes from where it was before backing up. |
||
< |
<syntaxhighlight lang="awk">#!/usr/bin/awk -f |
||
BEGIN { |
BEGIN { |
||
Line 735: | Line 735: | ||
print "" |
print "" |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example output: |
Example output: |
||
Line 745: | Line 745: | ||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="qbasic">dim a(0 to n-1) as integer |
||
'...more code... |
'...more code... |
||
sort: |
sort: |
||
Line 763: | Line 763: | ||
end if |
end if |
||
end if |
end if |
||
wend</ |
wend</syntaxhighlight> |
||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
{{works with|Windows NT}} |
{{works with|Windows NT}} |
||
< |
<syntaxhighlight lang="dos">@ECHO OFF |
||
SETLOCAL EnableExtensions EnableDelayedExpansion |
SETLOCAL EnableExtensions EnableDelayedExpansion |
||
:: GnomeSort.cmd in WinNT Batch using pseudo array. |
:: GnomeSort.cmd in WinNT Batch using pseudo array. |
||
Line 827: | Line 827: | ||
ENDLOCAL |
ENDLOCAL |
||
EXIT /B 0</ |
EXIT /B 0</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic">DEF PROC_GnomeSort1(Size%) |
||
I%=2 |
I%=2 |
||
J%=2 |
J%=2 |
||
Line 846: | Line 846: | ||
ENDIF |
ENDIF |
||
UNTIL I%>Size% |
UNTIL I%>Size% |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let gnomesort(A, len) be |
let gnomesort(A, len) be |
||
Line 881: | Line 881: | ||
gnomesort(array, length) |
gnomesort(array, length) |
||
writes("Output: ") ; writearray(array, length) ; wrch('*N') |
writes("Output: ") ; writearray(array, length) ; wrch('*N') |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Input: 52 -5 -20 199 65 -3 190 25 9999 -5342 |
<pre>Input: 52 -5 -20 199 65 -3 190 25 9999 -5342 |
||
Line 889: | Line 889: | ||
This algorithm sorts in place modifying the passed ''array'' (of <code>n</code> integer numbers). |
This algorithm sorts in place modifying the passed ''array'' (of <code>n</code> integer numbers). |
||
< |
<syntaxhighlight lang="c">void gnome_sort(int *a, int n) |
||
{ |
{ |
||
int i=1, j=2, t; |
int i=1, j=2, t; |
||
Line 901: | Line 901: | ||
} |
} |
||
# undef swap |
# undef swap |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp"> |
||
public static void gnomeSort(int[] anArray) |
public static void gnomeSort(int[] anArray) |
||
{ |
{ |
||
Line 933: | Line 933: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Uses C++11. Compile with |
Uses C++11. Compile with |
||
g++ -std=c++11 gnome.cpp |
g++ -std=c++11 gnome.cpp |
||
< |
<syntaxhighlight lang="cpp">#include <algorithm> |
||
#include <iterator> |
#include <iterator> |
||
#include <iostream> |
#include <iostream> |
||
Line 967: | Line 967: | ||
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " ")); |
||
std::cout << "\n"; |
std::cout << "\n"; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 975: | Line 975: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
{{trans|Haskell}} |
{{trans|Haskell}} |
||
< |
<syntaxhighlight lang="clojure">(defn gnomesort |
||
([c] (gnomesort c <)) |
([c] (gnomesort c <)) |
||
([c pred] |
([c pred] |
||
Line 986: | Line 986: | ||
(recur (concat x (list y1)) ys))))))) |
(recur (concat x (list y1)) ys))))))) |
||
(println (gnomesort [3 1 4 1 5 9 2 6 5]))</ |
(println (gnomesort [3 1 4 1 5 9 2 6 5]))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
Procedure division stuff only. |
Procedure division stuff only. |
||
< |
<syntaxhighlight lang="cobol"> C-SORT SECTION. |
||
C-000. |
C-000. |
||
DISPLAY "SORT STARTING". |
DISPLAY "SORT STARTING". |
||
Line 1,019: | Line 1,019: | ||
E-999. |
E-999. |
||
EXIT.</ |
EXIT.</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun gnome-sort (array predicate &aux (length (length array))) |
||
(do ((position (min 1 length))) |
(do ((position (min 1 length))) |
||
((eql length position) array) |
((eql length position) array) |
||
Line 1,034: | Line 1,034: | ||
(aref array (1- position))) |
(aref array (1- position))) |
||
(decf position)) |
(decf position)) |
||
(t (incf position)))))</ |
(t (incf position)))))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm; |
||
void gnomeSort(T)(T arr) { |
void gnomeSort(T)(T arr) { |
||
Line 1,060: | Line 1,060: | ||
gnomeSort(a); |
gnomeSort(a); |
||
writeln(a); |
writeln(a); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 1,066: | Line 1,066: | ||
Static array is an arbitrary-based array of fixed length |
Static array is an arbitrary-based array of fixed length |
||
< |
<syntaxhighlight lang="delphi">program TestGnomeSort; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 1,124: | Line 1,124: | ||
Writeln; |
Writeln; |
||
Readln; |
Readln; |
||
end.</ |
end.</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,132: | Line 1,132: | ||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
< |
<syntaxhighlight lang="delphi">procedure GnomeSort(a : array of Integer); |
||
var |
var |
||
i, j : Integer; |
i, j : Integer; |
||
Line 1,169: | Line 1,169: | ||
Print(Format('%3d ', [a[i]])); |
Print(Format('%3d ', [a[i]])); |
||
PrintLn('}'); |
PrintLn('}'); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Ouput : |
Ouput : |
||
<pre> |
<pre> |
||
Line 1,178: | Line 1,178: | ||
=={{header|E}}== |
=={{header|E}}== |
||
< |
<syntaxhighlight lang="e">def gnomeSort(array) { |
||
var size := array.size() |
var size := array.size() |
||
var i := 1 |
var i := 1 |
||
Line 1,197: | Line 1,197: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="e">? def a := [7,9,4,2,1,3,6,5,0,8].diverge() |
||
# value: [7, 9, 4, 2, 1, 3, 6, 5, 0, 8].diverge() |
# value: [7, 9, 4, 2, 1, 3, 6, 5, 0, 8].diverge() |
||
? gnomeSort(a) |
? gnomeSort(a) |
||
? a |
? a |
||
# value: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].diverge()</ |
# value: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].diverge()</syntaxhighlight> |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
GNOME_SORT [G -> COMPARABLE] |
GNOME_SORT [G -> COMPARABLE] |
||
Line 1,273: | Line 1,273: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test: |
Test: |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 1,310: | Line 1,310: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,321: | Line 1,321: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang="elena">import extensions; |
||
import system'routines; |
import system'routines; |
||
Line 1,361: | Line 1,361: | ||
console.printLine("before:", list.asEnumerable()); |
console.printLine("before:", list.asEnumerable()); |
||
console.printLine("after :", list.gnomeSort().asEnumerable()) |
console.printLine("after :", list.gnomeSort().asEnumerable()) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,370: | Line 1,370: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Erlang}} |
{{trans|Erlang}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Sort do |
||
def gnome_sort([]), do: [] |
def gnome_sort([]), do: [] |
||
def gnome_sort([h|t]), do: gnome_sort([h], t) |
def gnome_sort([h|t]), do: gnome_sort([h], t) |
||
Line 1,379: | Line 1,379: | ||
end |
end |
||
IO.inspect Sort.gnome_sort([8,3,9,1,3,2,6])</ |
IO.inspect Sort.gnome_sort([8,3,9,1,3,2,6])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,388: | Line 1,388: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">-module(gnome_sort). |
||
-export([gnome/1]). |
-export([gnome/1]). |
||
Line 1,396: | Line 1,396: | ||
gnome(P, [Next|N]) -> |
gnome(P, [Next|N]) -> |
||
gnome([Next|P], N). |
gnome([Next|P], N). |
||
gnome([H|T]) -> gnome([H], T).</ |
gnome([H|T]) -> gnome([H], T).</syntaxhighlight> |
||
< |
<syntaxhighlight lang="erlang">Eshell V5.7.3 (abort with ^G) |
||
1> c(gnome_sort). |
1> c(gnome_sort). |
||
{ok,gnome_sort} |
{ok,gnome_sort} |
||
2> gnome_sort:gnome([8,3,9,1,3,2,6]). |
2> gnome_sort:gnome([8,3,9,1,3,2,6]). |
||
[1,2,3,3,6,8,9] |
[1,2,3,3,6,8,9] |
||
3> </ |
3> </syntaxhighlight> |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">function gnomeSort(sequence s) |
||
integer i,j |
integer i,j |
||
object temp |
object temp |
||
Line 1,426: | Line 1,426: | ||
end while |
end while |
||
return s |
return s |
||
end function</ |
end function</syntaxhighlight> |
||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">let inline gnomeSort (a: _ []) = |
||
let rec loop i j = |
let rec loop i j = |
||
if i < a.Length then |
if i < a.Length then |
||
Line 1,437: | Line 1,437: | ||
a.[i] <- t |
a.[i] <- t |
||
if i=1 then loop j (j+1) else loop (i-1) j |
if i=1 then loop j (j+1) else loop (i-1) j |
||
loop 1 2</ |
loop 1 2</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 1,473: | Line 1,473: | ||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
{ |
{ |
||
Line 1,508: | Line 1,508: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 1,516: | Line 1,516: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">defer precedes |
||
defer exchange |
defer exchange |
||
Line 1,539: | Line 1,539: | ||
: .array 10 0 do example i cells + ? loop cr ; |
: .array 10 0 do example i cells + ? loop cr ; |
||
.array example 10 gnomesort .array</ |
.array example 10 gnomesort .array</syntaxhighlight> |
||
A slightly optimized version is presented below, where Gnome sort keeps track of its previous position. This reduces the number of iterations significantly. |
A slightly optimized version is presented below, where Gnome sort keeps track of its previous position. This reduces the number of iterations significantly. |
||
< |
<syntaxhighlight lang="forth">: gnomesort+ ( a n) |
||
swap >r 2 tuck 1- ( c2 n c1) |
swap >r 2 tuck 1- ( c2 n c1) |
||
begin ( c2 n c1) |
begin ( c2 n c1) |
||
Line 1,551: | Line 1,551: | ||
else drop >r dup 1+ swap r> swap then |
else drop >r dup 1+ swap r> swap then |
||
repeat drop drop drop r> drop |
repeat drop drop drop r> drop |
||
; ( --)</ |
; ( --)</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">program example |
||
implicit none |
implicit none |
||
Line 1,650: | Line 1,650: | ||
! |
! |
||
end program example</ |
end program example</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Used the task pseudo code as a base |
Used the task pseudo code as a base |
||
< |
<syntaxhighlight lang="freebasic">' version 21-10-2016 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
' for boundry checks on array's compile with: fbc -s console -exx |
' for boundry checks on array's compile with: fbc -s console -exx |
||
Line 1,704: | Line 1,704: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>unsort 4 -5 5 1 -3 -1 -2 -6 0 7 -4 6 2 -7 3 |
<pre>unsort 4 -5 5 1 -3 -1 -2 -6 0 7 -4 6 2 -7 3 |
||
Line 1,711: | Line 1,711: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=d91a871bd9f43cd9644c89baa3ee861a Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=d91a871bd9f43cd9644c89baa3ee861a Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim siCount As Short |
Dim siCount As Short |
||
Dim siCounti As Short = 1 |
Dim siCounti As Short = 1 |
||
Line 1,749: | Line 1,749: | ||
Return |
Return |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,757: | Line 1,757: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,780: | Line 1,780: | ||
j++ |
j++ |
||
} |
} |
||
}</ |
}</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,809: | Line 1,809: | ||
j++ |
j++ |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="groovy">def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] } |
||
def checkSwap = { list, i, j = i+1 -> [(list[i] > list[j])].find{ it }.each { makeSwap(list, i, j) } } |
def checkSwap = { list, i, j = i+1 -> [(list[i] > list[j])].find{ it }.each { makeSwap(list, i, j) } } |
||
Line 1,824: | Line 1,824: | ||
} |
} |
||
input |
input |
||
}</ |
}</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="groovy">println (gnomeSort([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4])) |
||
println (gnomeSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1]))</ |
println (gnomeSort([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,835: | Line 1,835: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">gnomeSort [] = [] |
||
gnomeSort (x:xs) = gs [x] xs |
gnomeSort (x:xs) = gs [x] xs |
||
where |
where |
||
Line 1,843: | Line 1,843: | ||
gs [] (y:ys) = gs [y] ys |
gs [] (y:ys) = gs [y] ys |
||
gs xs [] = reverse xs |
gs xs [] = reverse xs |
||
-- keeping the first argument of gs in reverse order avoids the deterioration into cubic behaviour </ |
-- keeping the first argument of gs in reverse order avoids the deterioration into cubic behaviour </syntaxhighlight> |
||
=={{header|Haxe}}== |
=={{header|Haxe}}== |
||
< |
<syntaxhighlight lang="haxe">class GnomeSort { |
||
@:generic |
@:generic |
||
public static function sort<T>(arr:Array<T>) { |
public static function sort<T>(arr:Array<T>) { |
||
Line 1,884: | Line 1,884: | ||
Sys.println('Sorted Strings: ' + stringArray); |
Sys.println('Sorted Strings: ' + stringArray); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,897: | Line 1,897: | ||
=={{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(gnomesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") |
demosort(gnomesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty") |
||
end |
end |
||
Line 1,915: | Line 1,915: | ||
} |
} |
||
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 1,928: | Line 1,928: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
Naive version: |
Naive version: |
||
< |
<syntaxhighlight lang="io">List do( |
||
gnomeSortInPlace := method( |
gnomeSortInPlace := method( |
||
idx := 1 |
idx := 1 |
||
Line 1,943: | Line 1,943: | ||
lst := list(5, -1, -4, 2, 9) |
lst := list(5, -1, -4, 2, 9) |
||
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</ |
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</syntaxhighlight> |
||
Optimized version, storing the position before traversing back towards the begining of the list ([[wp:Gnome_sort#Optimization|Wikipedia]]) |
Optimized version, storing the position before traversing back towards the begining of the list ([[wp:Gnome_sort#Optimization|Wikipedia]]) |
||
< |
<syntaxhighlight lang="io">List do( |
||
gnomeSortInPlace := method( |
gnomeSortInPlace := method( |
||
idx1 := 1 |
idx1 := 1 |
||
Line 1,964: | Line 1,964: | ||
lst := list(5, -1, -4, 2, 9) |
lst := list(5, -1, -4, 2, 9) |
||
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</ |
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</syntaxhighlight> |
||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
<syntaxhighlight lang="is-basic"> |
|||
<lang IS-BASIC> |
|||
100 PROGRAM "GnomeSrt.bas" |
100 PROGRAM "GnomeSrt.bas" |
||
110 RANDOMIZE |
110 RANDOMIZE |
||
Line 1,997: | Line 1,997: | ||
370 END IF |
370 END IF |
||
380 LOOP |
380 LOOP |
||
390 END DEF</ |
390 END DEF</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
{{eff note|J|/:~}} |
{{eff note|J|/:~}} |
||
< |
<syntaxhighlight lang="j">position=: 0 {.@I.@, [ |
||
swap=: ] A.~ *@position * #@[ !@- <:@position |
swap=: ] A.~ *@position * #@[ !@- <:@position |
||
gnome=: swap~ 2 >/\ ] |
gnome=: swap~ 2 >/\ ] |
||
gnomes=: gnome^:_</ |
gnomes=: gnome^:_</syntaxhighlight> |
||
Note 1: this implementation of swap is actually rather silly, and will not work on long lists. A better implementation would be:< |
Note 1: this implementation of swap is actually rather silly, and will not work on long lists. A better implementation would be:<syntaxhighlight lang="j">swap=: position (] {~ _1 0 + [)`(0 _1 + [)`]}^:(*@[) ]</syntaxhighlight> |
||
Note 2: this implementation only works for domains where > is defined (in J, "greater than" only works on numbers). To work around this issue, you could define:< |
Note 2: this implementation only works for domains where > is defined (in J, "greater than" only works on numbers). To work around this issue, you could define:<syntaxhighlight lang="j">gt=: -.@(-: /:~)@,&< |
||
gnome=: swap~ 2 gt/\ ]</ |
gnome=: swap~ 2 gt/\ ]</syntaxhighlight> |
||
Note 3: this implementation does not return intermediate results. If you want them, you could use< |
Note 3: this implementation does not return intermediate results. If you want them, you could use<syntaxhighlight lang="j">gnomeps=: gnome^:a:</syntaxhighlight> |
||
Note 4: gnomeps just shows intermediate results and does not show the gnome's position. You can extract the gnome's position using an expression such as<lang |
Note 4: gnomeps just shows intermediate results and does not show the gnome's position. You can extract the gnome's position using an expression such as<syntaxhighlight lang="j">2 ~:/\ gnomeps</syntaxhighlight>. |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="java">public static void gnomeSort(int[] a) |
||
{ |
{ |
||
int i=1; |
int i=1; |
||
Line 2,032: | Line 2,032: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">function gnomeSort(a) { |
||
function moveBack(i) { |
function moveBack(i) { |
||
for( ; i > 0 && a[i-1] > a[i]; i--) { |
for( ; i > 0 && a[i-1] > a[i]; i--) { |
||
Line 2,047: | Line 2,047: | ||
} |
} |
||
return a; |
return a; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 2,053: | Line 2,053: | ||
This implementation adheres to the specification. |
This implementation adheres to the specification. |
||
The array to be sorted, however, can be any JSON array. |
The array to be sorted, however, can be any JSON array. |
||
< |
<syntaxhighlight lang="jq"># As soon as "condition" is true, then emit . and stop: |
||
def do_until(condition; next): |
def do_until(condition; next): |
||
def u: if condition then . else (next|u) end; |
def u: if condition then . else (next|u) end; |
||
Line 2,076: | Line 2,076: | ||
end |
end |
||
end ) |
end ) |
||
| .[2];</ |
| .[2];</syntaxhighlight> |
||
'''Example''': |
'''Example''': |
||
< |
<syntaxhighlight lang="jq">[(2|sqrt), [1], null, 1, 0.5, 2, 1, -3, {"a": "A"}] | gnomeSort</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight lang="sh">$ jq -M -n -f Gnome_sort.jq |
||
[ |
[ |
||
null, |
null, |
||
Line 2,095: | Line 2,095: | ||
"a": "A" |
"a": "A" |
||
} |
} |
||
]</ |
]</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang="julia">function gnomesort!(arr::Vector) |
||
i, j = 1, 2 |
i, j = 1, 2 |
||
while i < length(arr) |
while i < length(arr) |
||
Line 2,119: | Line 2,119: | ||
v = rand(-10:10, 10) |
v = rand(-10:10, 10) |
||
println("# unordered: $v\n -> ordered: ", gnomesort!(v))</ |
println("# unordered: $v\n -> ordered: ", gnomesort!(v))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,126: | Line 2,126: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.0 |
||
fun <T: Comparable<T>> gnomeSort(a: Array<T>, ascending: Boolean = true) { |
fun <T: Comparable<T>> gnomeSort(a: Array<T>, ascending: Boolean = true) { |
||
Line 2,150: | Line 2,150: | ||
gnomeSort(array, false) |
gnomeSort(array, false) |
||
println("Sorted (desc) : ${array.asList()}") |
println("Sorted (desc) : ${array.asList()}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,161: | Line 2,161: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Keep in mind that Lua arrays initial index is 1. |
Keep in mind that Lua arrays initial index is 1. |
||
< |
<syntaxhighlight lang="lua">function gnomeSort(a) |
||
local i, j = 2, 3 |
local i, j = 2, 3 |
||
Line 2,177: | Line 2,177: | ||
end |
end |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="lua">list = { 5, 6, 1, 2, 9, 14, 2, 15, 6, 7, 8, 97 } |
||
gnomeSort(list) |
gnomeSort(list) |
||
for i, j in pairs(list) do |
for i, j in pairs(list) do |
||
print(j) |
print(j) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">arr := Array([17,3,72,0,36,2,3,8,40,0]): |
||
len := numelems(arr): |
len := numelems(arr): |
||
i := 2: |
i := 2: |
||
Line 2,199: | Line 2,199: | ||
end if: |
end if: |
||
end do: |
end do: |
||
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">gnomeSort[list_]:=Module[{i=2,j=3}, |
||
While[ i<=Length[[list]], |
While[ i<=Length[[list]], |
||
If[ list[[i-1]]<=list[[i]], |
If[ list[[i-1]]<=list[[i]], |
||
Line 2,210: | Line 2,210: | ||
list[[i-1;;i]]=list[[i;;i-1]];i--;]; |
list[[i-1;;i]]=list[[i;;i-1]];i--;]; |
||
If[i==1,i=j;j++;] |
If[i==1,i=j;j++;] |
||
]]</ |
]]</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">function list = gnomeSort(list) |
||
i = 2; |
i = 2; |
||
Line 2,233: | Line 2,233: | ||
end %while |
end %while |
||
end %gnomeSort</ |
end %gnomeSort</syntaxhighlight> |
||
Sample Usage: |
Sample Usage: |
||
< |
<syntaxhighlight lang="matlab">>> gnomeSort([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 gnomeSort arr = |
||
( |
( |
||
local i = 2 |
local i = 2 |
||
Line 2,266: | Line 2,266: | ||
) |
) |
||
return arr |
return arr |
||
)</ |
)</syntaxhighlight> |
||
Output: |
Output: |
||
<syntaxhighlight lang="maxscript"> |
|||
<lang MAXScript> |
|||
a = for i in 1 to 10 collect random 1 20 |
a = for i in 1 to 10 collect random 1 20 |
||
#(20, 10, 16, 2, 19, 12, 11, 3, 5, 16) |
#(20, 10, 16, 2, 19, 12, 11, 3, 5, 16) |
||
gnomesort a |
gnomesort a |
||
#(2, 3, 5, 10, 11, 12, 16, 16, 19, 20) |
#(2, 3, 5, 10, 11, 12, 16, 16, 19, 20) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Metafont}}== |
=={{header|Metafont}}== |
||
< |
<syntaxhighlight lang="metafont">def gnomesort(suffix v)(expr n) = |
||
begingroup save i, j, t; |
begingroup save i, j, t; |
||
i := 1; j := 2; |
i := 1; j := 2; |
||
Line 2,290: | Line 2,290: | ||
fi |
fi |
||
endfor |
endfor |
||
endgroup enddef;</ |
endgroup enddef;</syntaxhighlight> |
||
< |
<syntaxhighlight lang="metafont">numeric a[]; |
||
for i = 0 upto 9: a[i] := uniformdeviate(40); message decimal a[i]; endfor |
for i = 0 upto 9: a[i] := uniformdeviate(40); message decimal a[i]; endfor |
||
message char10; |
message char10; |
||
Line 2,298: | Line 2,298: | ||
gnomesort(a, 10); |
gnomesort(a, 10); |
||
for i = 0 upto 9: message decimal a[i]; endfor |
for i = 0 upto 9: message decimal a[i]; endfor |
||
end</ |
end</syntaxhighlight> |
||
=={{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,351: | Line 2,351: | ||
return ra |
return ra |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Output |
;Output |
||
<pre> |
<pre> |
||
Line 2,359: | Line 2,359: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">proc gnomeSort[T](a: var openarray[T]) = |
||
var |
var |
||
n = a.len |
n = a.len |
||
Line 2,374: | Line 2,374: | ||
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] |
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782] |
||
gnomeSort a |
gnomeSort 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> |
||
Line 2,380: | Line 2,380: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="objeck"> |
||
function : GnomeSort(a : Int[]) { |
function : GnomeSort(a : Int[]) { |
||
i := 1; |
i := 1; |
||
Line 2,402: | Line 2,402: | ||
}; |
}; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
from the toplevel: |
from the toplevel: |
||
< |
<syntaxhighlight lang="ocaml"># let gnome_sort a = |
||
let i = ref 1 |
let i = ref 1 |
||
and j = ref 2 in |
and j = ref 2 in |
||
Line 2,433: | Line 2,433: | ||
# a ;; |
# a ;; |
||
- : int array = [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|]</ |
- : int array = [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|]</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
< |
<syntaxhighlight lang="octave">function s = gnomesort(v) |
||
i = 2; j = 3; |
i = 2; j = 3; |
||
while ( i <= length(v) ) |
while ( i <= length(v) ) |
||
Line 2,454: | Line 2,454: | ||
endwhile |
endwhile |
||
s = v; |
s = v; |
||
endfunction</ |
endfunction</syntaxhighlight> |
||
< |
<syntaxhighlight lang="octave">v = [7, 9, 4, 2, 1, 3, 6, 5, 0, 8]; |
||
disp(gnomesort(v));</ |
disp(gnomesort(v));</syntaxhighlight> |
||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
===version 1=== |
===version 1=== |
||
{{Trans|NetRexx}} |
{{Trans|NetRexx}} |
||
< |
<syntaxhighlight lang="oorexx">/* Rexx */ |
||
call demo |
call demo |
||
Line 2,557: | Line 2,557: | ||
self~put(item, ix) |
self~put(item, ix) |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output: |
'''Output: |
||
<pre> |
<pre> |
||
Line 2,585: | Line 2,585: | ||
===version 2=== |
===version 2=== |
||
{{trans|REXX}} |
{{trans|REXX}} |
||
< |
<syntaxhighlight lang="oorexx">/* REXX --------------------------------------------------------------- |
||
* 28.06.2014 Walter Pachl |
* 28.06.2014 Walter Pachl |
||
* 30.06.2014 corrected in sync with REXX version 2 |
* 30.06.2014 corrected in sync with REXX version 2 |
||
Line 2,624: | Line 2,624: | ||
Say 'element' right(i,2) x[i] |
Say 'element' right(i,2) x[i] |
||
End |
End |
||
Return</ |
Return</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
Similar to REXX version 2 |
Similar to REXX version 2 |
||
Line 2,630: | Line 2,630: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
{{trans|Haskell}} |
{{trans|Haskell}} |
||
< |
<syntaxhighlight lang="oz">declare |
||
fun {GnomeSort Xs} |
fun {GnomeSort Xs} |
||
case Xs of nil then nil |
case Xs of nil then nil |
||
Line 2,646: | Line 2,646: | ||
end |
end |
||
in |
in |
||
{Show {GnomeSort [3 1 4 1 5 9 2 6 5]}}</ |
{Show {GnomeSort [3 1 4 1 5 9 2 6 5]}}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">gnomeSort(v)={ |
||
my(i=2,j=3,n=#v,t); |
my(i=2,j=3,n=#v,t); |
||
while(i<=n, |
while(i<=n, |
||
Line 2,663: | Line 2,663: | ||
); |
); |
||
v |
v |
||
};</ |
};</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">procedure gnomesort(var arr : Array of Integer; size : Integer); |
||
var |
var |
||
i, j, t : Integer; |
i, j, t : Integer; |
||
Line 2,689: | Line 2,689: | ||
end |
end |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
sub gnome_sort |
sub gnome_sort |
||
Line 2,715: | Line 2,715: | ||
} |
} |
||
return @a; |
return @a; |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="perl">my @arr = ( 10, 9, 8, 5, 2, 1, 1, 0, 50, -2 ); |
||
print "$_\n" foreach gnome_sort( @arr );</ |
print "$_\n" foreach gnome_sort( @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,746: | Line 2,746: | ||
<span style="color: #0000FF;">?</span><span style="color: #000000;">gnomeSort</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;">gnomeSort</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>--> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">function gnomeSort($arr){ |
||
$i = 1; |
$i = 1; |
||
$j = 2; |
$j = 2; |
||
Line 2,768: | Line 2,768: | ||
} |
} |
||
$arr = array(3,1,6,2,9,4,7,8,5); |
$arr = array(3,1,6,2,9,4,7,8,5); |
||
echo implode(',',gnomeSort($arr));</ |
echo implode(',',gnomeSort($arr));</syntaxhighlight> |
||
<pre>1,2,3,4,5,6,7,8,9</pre> |
<pre>1,2,3,4,5,6,7,8,9</pre> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de gnomeSort (Lst) |
||
(let J (cdr Lst) |
(let J (cdr Lst) |
||
(for (I Lst (cdr I)) |
(for (I Lst (cdr I)) |
||
Line 2,781: | Line 2,781: | ||
(setq I J J (cdr J)) |
(setq I J J (cdr J)) |
||
(setq I (prior I Lst)) ) ) ) ) |
(setq I (prior I Lst)) ) ) ) ) |
||
Lst )</ |
Lst )</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">SORT: PROCEDURE OPTIONS (MAIN); |
||
DECLARE A(0:9) FIXED STATIC INITIAL (5, 2, 7, 1, 9, 8, 6, 3, 4, 0); |
DECLARE A(0:9) FIXED STATIC INITIAL (5, 2, 7, 1, 9, 8, 6, 3, 4, 0); |
||
Line 2,811: | Line 2,811: | ||
END GNOME_SORT; |
END GNOME_SORT; |
||
END SORT;</ |
END SORT;</syntaxhighlight> |
||
Results: |
Results: |
||
<pre> |
<pre> |
||
Line 2,821: | Line 2,821: | ||
The [[#BASIC|BASIC]] example will work as-is if the array is declared in the same function as the sort. This example doesn't require that, but forces you to know your data type beforehand. |
The [[#BASIC|BASIC]] example will work as-is if the array is declared in the same function as the sort. This example doesn't require that, but forces you to know your data type beforehand. |
||
< |
<syntaxhighlight lang="powerbasic">SUB gnomeSort (a() AS LONG) |
||
DIM i AS LONG, j AS LONG |
DIM i AS LONG, j AS LONG |
||
i = 1 |
i = 1 |
||
Line 2,854: | Line 2,854: | ||
NEXT |
NEXT |
||
CLOSE 1 |
CLOSE 1 |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
Line 2,861: | Line 2,861: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function gnomesort($a) { |
function gnomesort($a) { |
||
$size, $i, $j = $a.Count, 1, 2 |
$size, $i, $j = $a.Count, 1, 2 |
||
Line 2,882: | Line 2,882: | ||
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11) |
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11) |
||
"$(gnomesort $array)" |
"$(gnomesort $array)" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
Line 2,889: | Line 2,889: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure GnomeSort(Array a(1)) |
||
Protected Size = ArraySize(a()) + 1 |
Protected Size = ArraySize(a()) + 1 |
||
Protected i = 1, j = 2 |
Protected i = 1, j = 2 |
||
Line 2,907: | Line 2,907: | ||
EndIf |
EndIf |
||
Wend |
Wend |
||
EndProcedure</ |
EndProcedure</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">>>> def gnomesort(a): |
||
i,j,size = 1,2,len(a) |
i,j,size = 1,2,len(a) |
||
while i < size: |
while i < size: |
||
Line 2,924: | Line 2,924: | ||
>>> gnomesort([3,4,2,5,1,6]) |
>>> gnomesort([3,4,2,5,1,6]) |
||
[1, 2, 3, 4, 5, 6] |
[1, 2, 3, 4, 5, 6] |
||
>>></ |
>>></syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery">[ dup size times |
||
[ i^ 0 > if |
[ i^ 0 > if |
||
[ dup i^ 1 - peek |
[ dup i^ 1 - peek |
||
Line 2,936: | Line 2,936: | ||
swap i^ 1 - poke |
swap i^ 1 - poke |
||
-1 step ] |
-1 step ] |
||
else 2drop ] ] ] is gnomesort ( [ --> [ )</ |
else 2drop ] ] ] is gnomesort ( [ --> [ )</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
===Starting from 1=== |
===Starting from 1=== |
||
< |
<syntaxhighlight lang="rsplus">gnomesort <- function(x) |
||
{ |
{ |
||
i <- 1 |
i <- 1 |
||
Line 2,965: | Line 2,965: | ||
x |
x |
||
} |
} |
||
gnomesort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782</ |
gnomesort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782</syntaxhighlight> |
||
===Starting from 2=== |
===Starting from 2=== |
||
Line 2,971: | Line 2,971: | ||
As R is 1-indexed, we need to make some minor adjustments to the given pseudo-code. To give some variety and to remove the previous solution's potentially redundant first run, we have chosen a different adjustment to the previous solution's. We have otherwise aimed to be faithful to the pseudo-code. |
As R is 1-indexed, we need to make some minor adjustments to the given pseudo-code. To give some variety and to remove the previous solution's potentially redundant first run, we have chosen a different adjustment to the previous solution's. We have otherwise aimed to be faithful to the pseudo-code. |
||
< |
<syntaxhighlight lang="rsplus">gnomeSort <- function(a) |
||
{ |
{ |
||
i <- 2 |
i <- 2 |
||
Line 2,999: | Line 2,999: | ||
ints <- c(1, 10, 2, 5, -1, 5, -19, 4, 23, 0) |
ints <- c(1, 10, 2, 5, -1, 5, -19, 4, 23, 0) |
||
numerics <- c(1, -3.2, 5.2, 10.8, -5.7, 7.3, 3.5, 0, -4.1, -9.5) |
numerics <- c(1, -3.2, 5.2, 10.8, -5.7, 7.3, 3.5, 0, -4.1, -9.5) |
||
strings <- c("We", "hold", "these", "truths", "to", "be", "self-evident", "that", "all", "men", "are", "created", "equal")</ |
strings <- c("We", "hold", "these", "truths", "to", "be", "self-evident", "that", "all", "men", "are", "created", "equal")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,011: | Line 3,011: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 3,046: | Line 3,046: | ||
[else `(,ps ,n ,p . ,ns)]))]))])) |
[else `(,ps ,n ,p . ,ns)]))]))])) |
||
(gnome-sort2 '(3 2 1 4 5 6) <=) |
(gnome-sort2 '(3 2 1 4 5 6) <=) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 3,052: | Line 3,052: | ||
{{Works with|rakudo|2016.03}} |
{{Works with|rakudo|2016.03}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub gnome_sort (@a) { |
||
my ($i, $j) = 1, 2; |
my ($i, $j) = 1, 2; |
||
while $i < @a { |
while $i < @a { |
||
Line 3,064: | Line 3,064: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
my @n = (1..10).roll(20); |
my @n = (1..10).roll(20); |
||
Line 3,070: | Line 3,070: | ||
=={{header|Rascal}}== |
=={{header|Rascal}}== |
||
< |
<syntaxhighlight lang="rascal">import List; |
||
public list[int] gnomeSort(a){ |
public list[int] gnomeSort(a){ |
||
Line 3,088: | Line 3,088: | ||
j += 1;}}} |
j += 1;}}} |
||
return a; |
return a; |
||
}</ |
}</syntaxhighlight> |
||
An example output: |
An example output: |
||
< |
<syntaxhighlight lang="rascal">gnomeSort([4, 65, 2, -31, 0, 99, 83, 782, 1]) |
||
list[int]: [-31,0,1,2,4,65,83,99,782]</ |
list[int]: [-31,0,1,2,4,65,83,99,782]</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
This REXX version uses an unity-based array (instead of a zero-based array). |
This REXX version uses an unity-based array (instead of a zero-based array). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program sorts an array using the gnome sort algorithm (elements contain blanks). */ |
||
call gen /*generate the @ stemmed array. */ |
call gen /*generate the @ stemmed array. */ |
||
call show 'before sort' /*display the before array elements.*/ |
call show 'before sort' /*display the before array elements.*/ |
||
Line 3,118: | Line 3,118: | ||
end /*j*/; return |
end /*j*/; return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: do j=1 for #; say ' element' right(j, w) arg(1)":" @.j; end; return</ |
show: do j=1 for #; say ' element' right(j, w) arg(1)":" @.j; end; return</syntaxhighlight> |
||
{{out|output|:}} |
{{out|output|:}} |
||
Line 3,145: | Line 3,145: | ||
===version 2=== |
===version 2=== |
||
< |
<syntaxhighlight lang="rexx">/* REXX --------------------------------------------------------------- |
||
* 28.06.2014 Walter Pachl cf ooRexx version 2 |
* 28.06.2014 Walter Pachl cf ooRexx version 2 |
||
* only style changes (reformatting) and adapt for ooRexx compatibility |
* only style changes (reformatting) and adapt for ooRexx compatibility |
||
Line 3,191: | Line 3,191: | ||
End |
End |
||
Return |
Return |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''output''' |
'''output''' |
||
<pre>before sort |
<pre>before sort |
||
Line 3,215: | Line 3,215: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
aList = [ 5, 6, 1, 2, 9, 14, 15, 7, 8, 97] |
aList = [ 5, 6, 1, 2, 9, 14, 15, 7, 8, 97] |
||
gnomeSort(aList) |
gnomeSort(aList) |
||
Line 3,237: | Line 3,237: | ||
i = j |
i = j |
||
j = j + 1 ok ok end |
j = j + 1 ok ok end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">class Array |
||
def gnomesort! |
def gnomesort! |
||
i, j = 1, 2 |
i, j = 1, 2 |
||
Line 3,259: | Line 3,259: | ||
ary = [7,6,5,9,8,4,3,1,2,0] |
ary = [7,6,5,9,8,4,3,1,2,0] |
||
ary.gnomesort! |
ary.gnomesort! |
||
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</ |
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn gnome_sort<T: PartialOrd>(a: &mut [T]) { |
||
let len = a.len(); |
let len = a.len(); |
||
let mut i: usize = 1; |
let mut i: usize = 1; |
||
Line 3,287: | Line 3,287: | ||
gnome_sort(&mut v); |
gnome_sort(&mut v); |
||
println!("after: {:?}", v); |
println!("after: {:?}", v); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,296: | Line 3,296: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">object GnomeSort { |
||
def gnomeSort(a: Array[Int]): Unit = { |
def gnomeSort(a: Array[Int]): Unit = { |
||
var (i, j) = (1, 2) |
var (i, j) = (1, 2) |
||
Line 3,308: | Line 3,308: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">; supply comparison function, which returns true if first and second |
||
; arguments are in order or equal. |
; arguments are in order or equal. |
||
(define (gnome-sort-compar in-order input-list) |
(define (gnome-sort-compar in-order input-list) |
||
Line 3,331: | Line 3,331: | ||
(gnome |
(gnome |
||
(cdr p) ; Prev list shorter by one |
(cdr p) ; Prev list shorter by one |
||
(cons next-pot (cons prev-pot (cdr n))))))))))</ |
(cons next-pot (cons prev-pot (cdr n))))))))))</syntaxhighlight> |
||
#;1> (gnome-sort-compar <= '(98 36 2 78 5 81 32 90 73 21 94 28 53 25 10 99)) |
#;1> (gnome-sort-compar <= '(98 36 2 78 5 81 32 90 73 21 94 28 53 25 10 99)) |
||
(2 5 10 21 25 28 32 36 53 73 78 81 90 94 98 99) |
(2 5 10 21 25 28 32 36 53 73 78 81 90 94 98 99) |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">class Array { |
||
method gnomesort { |
method gnomesort { |
||
var (i=1, j=2); |
var (i=1, j=2); |
||
Line 3,356: | Line 3,356: | ||
var ary = [7,6,5,9,8,4,3,1,2,0]; |
var ary = [7,6,5,9,8,4,3,1,2,0]; |
||
say ary.gnomesort;</ |
say ary.gnomesort;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</pre> |
<pre>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</pre> |
||
Line 3,362: | Line 3,362: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
< |
<syntaxhighlight lang="smalltalk">Smalltalk at: #gnomesort put: nil. |
||
"Utility" |
"Utility" |
||
Line 3,394: | Line 3,394: | ||
] |
] |
||
] |
] |
||
].</ |
].</syntaxhighlight> |
||
'''Testing''' |
'''Testing''' |
||
< |
<syntaxhighlight lang="smalltalk">|r o| |
||
r := Random new. |
r := Random new. |
||
o := OrderedCollection new. |
o := OrderedCollection new. |
||
Line 3,406: | Line 3,406: | ||
gnomesort value: o. |
gnomesort value: o. |
||
1 to: 10 do: [ :i | (o at: i) displayNl ].</ |
1 to: 10 do: [ :i | (o at: i) displayNl ].</syntaxhighlight> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Line 3,412: | Line 3,412: | ||
Implementation of the Gnome sort. Note this is an overengineered approach that performs many checks the real world would need but might obfuscate intent. As such the actual implementation is carefully labelled and the rest can be ignored except as interest dictates. |
Implementation of the Gnome sort. Note this is an overengineered approach that performs many checks the real world would need but might obfuscate intent. As such the actual implementation is carefully labelled and the rest can be ignored except as interest dictates. |
||
< |
<syntaxhighlight lang="snobol4">*** GNOME SORT ***************************************************************** |
||
* GNOME(V) -- gnome sort of the numerical vector V. |
* GNOME(V) -- gnome sort of the numerical vector V. |
||
*** HELPER FUNCTIONS *********************************************************** |
*** HELPER FUNCTIONS *********************************************************** |
||
Line 3,474: | Line 3,474: | ||
prototype(v) ',' :S(FRETURN)F(RETURN) |
prototype(v) ',' :S(FRETURN)F(RETURN) |
||
**************************************** |
**************************************** |
||
GNOME.END</ |
GNOME.END</syntaxhighlight> |
||
The test script looks like this: |
The test script looks like this: |
||
< |
<syntaxhighlight lang="snobol4">-INCLUDE 'gnome_sort.sno' |
||
GNOME.TEST output = 'valid values...' |
GNOME.TEST output = 'valid values...' |
||
Line 3,505: | Line 3,505: | ||
output = 'PASSED!' |
output = 'PASSED!' |
||
END</ |
END</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func gnomeSort<T: Comparable>(_ a: inout [T]) { |
||
var i = 1 |
var i = 1 |
||
var j = 2 |
var j = 2 |
||
Line 3,529: | Line 3,529: | ||
print("before: \(array)") |
print("before: \(array)") |
||
gnomeSort(&array) |
gnomeSort(&array) |
||
print(" after: \(array)")</ |
print(" after: \(array)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,539: | Line 3,539: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{tcllib|struct::list}} |
{{tcllib|struct::list}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
package require struct::list |
package require struct::list |
||
Line 3,562: | Line 3,562: | ||
} |
} |
||
puts [gnomesort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</ |
puts [gnomesort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
Line 3,597: | Line 3,597: | ||
=={{header|True BASIC}}== |
=={{header|True BASIC}}== |
||
{{trans|IS-BASIC}} |
{{trans|IS-BASIC}} |
||
< |
<syntaxhighlight lang="qbasic"> |
||
RANDOMIZE !RAMDOMZE TIMER en QBASIC |
RANDOMIZE !RAMDOMZE TIMER en QBASIC |
||
DIM array(-5 TO 12) |
DIM array(-5 TO 12) |
||
Line 3,641: | Line 3,641: | ||
NEXT i |
NEXT i |
||
END SUB |
END SUB |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
<lang>PRINT "Gnome sort:" |
<syntaxhighlight lang="text">PRINT "Gnome sort:" |
||
n = FUNC (_InitArray) |
n = FUNC (_InitArray) |
||
PROC _ShowArray (n) |
PROC _ShowArray (n) |
||
Line 3,697: | Line 3,697: | ||
PRINT |
PRINT |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 3,704: | Line 3,704: | ||
finding an item out of order, bubble it backwards to its |
finding an item out of order, bubble it backwards to its |
||
proper position and resume scanning forward from where it was. |
proper position and resume scanning forward from where it was. |
||
< |
<syntaxhighlight lang="ursala">gnome_sort "p" = |
||
@NiX ^=lx -+ # iteration |
@NiX ^=lx -+ # iteration |
||
~&r?\~& @lNXrX ->llx2rhPlrPCTxPrtPX~&lltPlhPrCXPrX ~&ll&& @llPrXh not "p", # backward bubble |
~&r?\~& @lNXrX ->llx2rhPlrPCTxPrtPX~&lltPlhPrCXPrX ~&ll&& @llPrXh not "p", # backward bubble |
||
->~&rhPlCrtPX ~&r&& ~&lZ!| @bh "p"+- # forward scan</ |
->~&rhPlCrtPX ~&r&& ~&lZ!| @bh "p"+- # forward scan</syntaxhighlight> |
||
Most of the code is wasted on dull but unavoidable stack manipulations. |
Most of the code is wasted on dull but unavoidable stack manipulations. |
||
Here is a test program using the lexical partial order relation. |
Here is a test program using the lexical partial order relation. |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
#cast %s |
#cast %s |
||
t = gnome_sort(lleq) 'dfijhkwlawfkjnksdjnoqwjefgflkdsgioi'</ |
t = gnome_sort(lleq) 'dfijhkwlawfkjnksdjnoqwjefgflkdsgioi'</syntaxhighlight> |
||
output: |
output: |
||
<pre> |
<pre> |
||
Line 3,721: | Line 3,721: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}< |
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function gnomeSort(s As Variant) As Variant |
||
Dim i As Integer: i = 1 |
Dim i As Integer: i = 1 |
||
Dim j As Integer: j = 2 |
Dim j As Integer: j = 2 |
||
Line 3,745: | Line 3,745: | ||
Public Sub main() |
Public Sub main() |
||
Debug.Print Join(gnomeSort([{5, 3, 1, 7, 4, 1, 1, 20}]), ", ") |
Debug.Print Join(gnomeSort([{5, 3, 1, 7, 4, 1, 1, 20}]), ", ") |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre>1, 1, 1, 3, 4, 5, 7, 20</pre> |
<pre>1, 1, 1, 3, 4, 5, 7, 20</pre> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
====Implementation==== |
====Implementation==== |
||
< |
<syntaxhighlight lang="vb">function gnomeSort( a ) |
||
dim i |
dim i |
||
dim j |
dim j |
||
Line 3,776: | Line 3,776: | ||
x = y |
x = y |
||
y = temp |
y = temp |
||
end sub</ |
end sub</syntaxhighlight> |
||
====Invocation==== |
====Invocation==== |
||
< |
<syntaxhighlight lang="vb">dim a |
||
dim b |
dim b |
||
Line 3,799: | Line 3,799: | ||
a = array( now(), now()-1,now()+2) |
a = array( now(), now()-1,now()+2) |
||
b = gnomeSort( a ) |
b = gnomeSort( a ) |
||
wscript.echo join(a, ", ")</ |
wscript.echo join(a, ", ")</syntaxhighlight> |
||
====Output==== |
====Output==== |
||
< |
<syntaxhighlight lang="vbscript">aardvark, ampicillin, gogodala, wolverhampton, zanzibar, zulu |
||
23, 89, 234, 345, 456, 456, 547, 567, 567, 568, 649, 2345, 5769 |
23, 89, 234, 345, 456, 456, 547, 567, 567, 568, 649, 2345, 5769 |
||
True, True, True, True, False, False, False, False |
True, True, True, True, False, False, False, False |
||
-45.99, -3, 1, 2, 2, 4, 67789 |
-45.99, -3, 1, 2, 2, 4, 67789 |
||
2/02/2010 10:19:51 AM, 3/02/2010 10:19:51 AM, 5/02/2010 10:19:51 AM</ |
2/02/2010 10:19:51 AM, 3/02/2010 10:19:51 AM, 5/02/2010 10:19:51 AM</syntaxhighlight> |
||
Note: All data in VBScript is of type Variant. Thus the code can sort many different types of data without code modification. |
Note: All data in VBScript is of type Variant. Thus the code can sort many different types of data without code modification. |
||
=={{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,830: | Line 3,830: | ||
j++ |
j++ |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>before: [170, 45, 75, -90, -802, 24, 2, 66] |
<pre>before: [170, 45, 75, -90, -802, 24, 2, 66] |
||
Line 3,837: | Line 3,837: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">var gnomeSort = Fn.new { |a, asc| |
||
var size = a.count |
var size = a.count |
||
var i = 1 |
var i = 1 |
||
Line 3,869: | Line 3,869: | ||
System.print() |
System.print() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,891: | Line 3,891: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">code ChOut=8, IntOut=11; |
||
proc GnomeSort(A(0..Size-1)); \Sort array A |
proc GnomeSort(A(0..Size-1)); \Sort array A |
||
Line 3,918: | Line 3,918: | ||
GnomeSort(A, 10); |
GnomeSort(A, 10); |
||
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )]; |
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,926: | Line 3,926: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn gnomeSort(a){ |
||
i,j,size := 1,2,a.len(); |
i,j,size := 1,2,a.len(); |
||
while(i < size){ |
while(i < size){ |
||
Line 3,938: | Line 3,938: | ||
}//while |
}//while |
||
a |
a |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">gnomeSort("This is a test".split("")).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>L(" "," "," ","T","a","e","h","i","i","s","s","s","t","t")</pre> |
<pre>L(" "," "," ","T","a","e","h","i","i","s","s","s","t","t")</pre> |