Sorting algorithms/Gnome sort: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 34:
{{trans|Python}}
<
V i = 1
V j = 2
Line 49:
R a
print(gnomesort(&[3, 4, 2, 5, 1, 6]))</
{{out}}
Line 57:
=={{header|360 Assembly}}==
<
GNOME CSECT
USING GNOME,R13 base register
Line 112:
PG DC CL125' ' buffer
REGEQU
END GNOME</
{{out}}
<pre>
Line 120:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program gnomeSort64.s */
Line 286:
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Action!}}==
<
INT i
Line 342:
Test(c,8)
Test(d,12)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Gnome_sort.png Screenshot from Atari 8-bit computer]
Line 368:
=={{header|ActionScript}}==
<
{
var pos:uint = 0;
Line 384:
}
return array;
}</
=={{header|Ada}}==
This example is a generic procedure for constrained array types.
<
type Element_Type is private;
type Index is (<>);
type Collection is array(Index) of Element_Type;
with function "<=" (Left, Right : Element_Type) return Boolean is <>;
procedure Gnome_Sort(Item : in out Collection);</
<
procedure Swap(Left, Right : in out Element_Type) is
Temp : Element_Type := Left;
Line 419:
end if;
end loop;
end Gnome_Sort;</
Usage example:
<
with Ada.Text_Io; use Ada.Text_Io;
Line 439:
end loop;
New_Line;
end Gnome_Sort_Test;</
=={{header|ALGOL 68}}==
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}}
<
PROC inplace gnome sort = (REF[]SORTSTRUCT list)REF[]SORTSTRUCT:
Line 468:
[]SORTSTRUCT char array data = "big fjords vex quick waltz nymph";
print((gnome sort(char array data), new line))</
Output:
<pre>
Line 475:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program gnomeSort.s */
Line 632:
.include "../affichage.inc"
</syntaxhighlight>
=={{header|Arturo}}==
<
i: 1
j: 2
Line 660:
]
print gnomeSort [3 1 2 8 5 7 9 4 6]</
{{out}}
Line 668:
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276379.html#276379 forum]
<
MsgBox % GnomeSort("xxx")
MsgBox % GnomeSort("3,2,1")
Line 689:
sorted .= "," . a%A_Index%
Return SubStr(sorted,2) ; drop leading comma
}</
=={{header|AWK}}==
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.
<
BEGIN {
Line 735:
print ""
}
</syntaxhighlight>
Example output:
Line 745:
{{trans|C}}
<
'...more code...
sort:
Line 763:
end if
end if
wend</
=={{header|Batch File}}==
{{works with|Windows NT}}
<
SETLOCAL EnableExtensions EnableDelayedExpansion
:: GnomeSort.cmd in WinNT Batch using pseudo array.
Line 827:
ENDLOCAL
EXIT /B 0</
=={{header|BBC BASIC}}==
<
I%=2
J%=2
Line 846:
ENDIF
UNTIL I%>Size%
ENDPROC</
=={{header|BCPL}}==
<
let gnomesort(A, len) be
Line 881:
gnomesort(array, length)
writes("Output: ") ; writearray(array, length) ; wrch('*N')
$)</
{{out}}
<pre>Input: 52 -5 -20 199 65 -3 190 25 9999 -5342
Line 889:
This algorithm sorts in place modifying the passed ''array'' (of <code>n</code> integer numbers).
<
{
int i=1, j=2, t;
Line 901:
}
# undef swap
}</
=={{header|C sharp|C#}}==
<
public static void gnomeSort(int[] anArray)
{
Line 933:
}
}
</syntaxhighlight>
=={{header|C++}}==
Uses C++11. Compile with
g++ -std=c++11 gnome.cpp
<
#include <iterator>
#include <iostream>
Line 967:
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}</
Output:
<pre>
Line 975:
=={{header|Clojure}}==
{{trans|Haskell}}
<
([c] (gnomesort c <))
([c pred]
Line 986:
(recur (concat x (list y1)) ys)))))))
(println (gnomesort [3 1 4 1 5 9 2 6 5]))</
=={{header|COBOL}}==
Procedure division stuff only.
<
C-000.
DISPLAY "SORT STARTING".
Line 1,019:
E-999.
EXIT.</
=={{header|Common Lisp}}==
<
(do ((position (min 1 length)))
((eql length position) array)
Line 1,034:
(aref array (1- position)))
(decf position))
(t (incf position)))))</
=={{header|D}}==
<
void gnomeSort(T)(T arr) {
Line 1,060:
gnomeSort(a);
writeln(a);
}</
=={{header|Delphi}}==
Line 1,066:
Static array is an arbitrary-based array of fixed length
<
{$APPTYPE CONSOLE}
Line 1,124:
Writeln;
Readln;
end.</
Output:
<pre>
Line 1,132:
=={{header|DWScript}}==
<
var
i, j : Integer;
Line 1,169:
Print(Format('%3d ', [a[i]]));
PrintLn('}');
</syntaxhighlight>
Ouput :
<pre>
Line 1,178:
=={{header|E}}==
<
var size := array.size()
var i := 1
Line 1,197:
}
}
}</
<
# value: [7, 9, 4, 2, 1, 3, 6, 5, 0, 8].diverge()
? gnomeSort(a)
? a
# value: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].diverge()</
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
GNOME_SORT [G -> COMPARABLE]
Line 1,273:
end
</syntaxhighlight>
Test:
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 1,310:
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,321:
=={{header|Elena}}==
ELENA 5.0 :
<
import system'routines;
Line 1,361:
console.printLine("before:", list.asEnumerable());
console.printLine("after :", list.gnomeSort().asEnumerable())
}</
{{out}}
<pre>
Line 1,370:
=={{header|Elixir}}==
{{trans|Erlang}}
<
def gnome_sort([]), do: []
def gnome_sort([h|t]), do: gnome_sort([h], t)
Line 1,379:
end
IO.inspect Sort.gnome_sort([8,3,9,1,3,2,6])</
{{out}}
Line 1,388:
=={{header|Erlang}}==
<
-export([gnome/1]).
Line 1,396:
gnome(P, [Next|N]) ->
gnome([Next|P], N).
gnome([H|T]) -> gnome([H], T).</
<
1> c(gnome_sort).
{ok,gnome_sort}
2> gnome_sort:gnome([8,3,9,1,3,2,6]).
[1,2,3,3,6,8,9]
3> </
=={{header|Euphoria}}==
<
integer i,j
object temp
Line 1,426:
end while
return s
end function</
=={{header|F Sharp|F#}}==
<
let rec loop i j =
if i < a.Length then
Line 1,437:
a.[i] <- t
if i=1 then loop j (j+1) else loop (i-1) j
loop 1 2</
=={{header|Factor}}==
Line 1,473:
=={{header|Fantom}}==
<
class Main
{
Line 1,508:
}
}
</syntaxhighlight>
Output:
Line 1,516:
=={{header|Forth}}==
<
defer exchange
Line 1,539:
: .array 10 0 do example i cells + ? loop cr ;
.array example 10 gnomesort .array</
A slightly optimized version is presented below, where Gnome sort keeps track of its previous position. This reduces the number of iterations significantly.
<
swap >r 2 tuck 1- ( c2 n c1)
begin ( c2 n c1)
Line 1,551:
else drop >r dup 1+ swap r> swap then
repeat drop drop drop r> drop
; ( --)</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 1,650:
!
end program example</
=={{header|FreeBASIC}}==
Used the task pseudo code as a base
<
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
Line 1,704:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>unsort 4 -5 5 1 -3 -1 -2 -6 0 7 -4 6 2 -7 3
Line 1,711:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=d91a871bd9f43cd9644c89baa3ee861a Click this link to run this code]'''
<
Dim siCount As Short
Dim siCounti As Short = 1
Line 1,749:
Return
End</
Output:
<pre>
Line 1,757:
=={{header|Go}}==
<
import "fmt"
Line 1,780:
j++
}
}</
More generic version that sorts anything that implements <code>sort.Interface</code>:
<
import (
Line 1,809:
j++
}
}</
=={{header|Groovy}}==
Solution:
<
def checkSwap = { list, i, j = i+1 -> [(list[i] > list[j])].find{ it }.each { makeSwap(list, i, j) } }
Line 1,824:
}
input
}</
Test:
<
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]))</
Output:
Line 1,835:
=={{header|Haskell}}==
<
gnomeSort (x:xs) = gs [x] xs
where
Line 1,843:
gs [] (y:ys) = gs [y] ys
gs xs [] = reverse xs
-- keeping the first argument of gs in reverse order avoids the deterioration into cubic behaviour </
=={{header|Haxe}}==
<
@:generic
public static function sort<T>(arr:Array<T>) {
Line 1,884:
Sys.println('Sorted Strings: ' + stringArray);
}
}</
{{out}}
Line 1,897:
=={{header|Icon}} and {{header|Unicon}}==
<
demosort(gnomesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
Line 1,915:
}
return X
end</
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]]. The full demosort exercises the named sort of a list with op = "numeric", "string", ">>" (lexically gt, descending),">" (numerically gt, descending), a custom comparator, and also a string.
Line 1,928:
=={{header|Io}}==
Naive version:
<
gnomeSortInPlace := method(
idx := 1
Line 1,943:
lst := list(5, -1, -4, 2, 9)
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</
Optimized version, storing the position before traversing back towards the begining of the list ([[wp:Gnome_sort#Optimization|Wikipedia]])
<
gnomeSortInPlace := method(
idx1 := 1
Line 1,964:
lst := list(5, -1, -4, 2, 9)
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">
100 PROGRAM "GnomeSrt.bas"
110 RANDOMIZE
Line 1,997:
370 END IF
380 LOOP
390 END DEF</
=={{header|J}}==
{{eff note|J|/:~}}
<
swap=: ] A.~ *@position * #@[ !@- <:@position
gnome=: swap~ 2 >/\ ]
gnomes=: gnome^:_</
Note 1: this implementation of swap is actually rather silly, and will not work on long lists. A better implementation would be:<
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:<
gnome=: swap~ 2 gt/\ ]</
Note 3: this implementation does not return intermediate results. If you want them, you could use<
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
=={{header|Java}}==
{{trans|C}}
<
{
int i=1;
Line 2,032:
}
}
}</
=={{header|JavaScript}}==
<
function moveBack(i) {
for( ; i > 0 && a[i-1] > a[i]; i--) {
Line 2,047:
}
return a;
}</
=={{header|jq}}==
Line 2,053:
This implementation adheres to the specification.
The array to be sorted, however, can be any JSON array.
<
def do_until(condition; next):
def u: if condition then . else (next|u) end;
Line 2,076:
end
end )
| .[2];</
'''Example''':
<
{{Out}}
<
[
null,
Line 2,095:
"a": "A"
}
]</
=={{header|Julia}}==
{{works with|Julia|0.6}}
<
i, j = 1, 2
while i < length(arr)
Line 2,119:
v = rand(-10:10, 10)
println("# unordered: $v\n -> ordered: ", gnomesort!(v))</
{{out}}
Line 2,126:
=={{header|Kotlin}}==
<
fun <T: Comparable<T>> gnomeSort(a: Array<T>, ascending: Boolean = true) {
Line 2,150:
gnomeSort(array, false)
println("Sorted (desc) : ${array.asList()}")
}</
{{out}}
Line 2,161:
=={{header|Lua}}==
Keep in mind that Lua arrays initial index is 1.
<
local i, j = 2, 3
Line 2,177:
end
end
end</
Example:
<
gnomeSort(list)
for i, j in pairs(list) do
print(j)
end</
=={{header|Maple}}==
<
len := numelems(arr):
i := 2:
Line 2,199:
end if:
end do:
arr;</
{{Out|Output}}
<pre>[0,0,2,3,3,8,17,36,40,72]</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
While[ i<=Length[[list]],
If[ list[[i-1]]<=list[[i]],
Line 2,210:
list[[i-1;;i]]=list[[i;;i-1]];i--;];
If[i==1,i=j;j++;]
]]</
=={{header|MATLAB}} / {{header|Octave}}==
<
i = 2;
Line 2,233:
end %while
end %gnomeSort</
Sample Usage:
<
ans =
1 2 3 4 5 6</
=={{header|MAXScript}}==
<
(
local i = 2
Line 2,266:
)
return arr
)</
Output:
<syntaxhighlight lang="maxscript">
a = for i in 1 to 10 collect random 1 20
#(20, 10, 16, 2, 19, 12, 11, 3, 5, 16)
gnomesort a
#(2, 3, 5, 10, 11, 12, 16, 16, 19, 20)
</syntaxhighlight>
=={{header|Metafont}}==
<
begingroup save i, j, t;
i := 1; j := 2;
Line 2,290:
fi
endfor
endgroup enddef;</
<
for i = 0 upto 9: a[i] := uniformdeviate(40); message decimal a[i]; endfor
message char10;
Line 2,298:
gnomesort(a, 10);
for i = 0 upto 9: message decimal a[i]; endfor
end</
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols binary
Line 2,351:
return ra
</syntaxhighlight>
;Output
<pre>
Line 2,359:
=={{header|Nim}}==
<
var
n = a.len
Line 2,374:
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
gnomeSort a
echo a</
Output:
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre>
Line 2,380:
=={{header|Objeck}}==
{{trans|C}}
<
function : GnomeSort(a : Int[]) {
i := 1;
Line 2,402:
};
}
</syntaxhighlight>
=={{header|OCaml}}==
from the toplevel:
<
let i = ref 1
and j = ref 2 in
Line 2,433:
# a ;;
- : int array = [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|]</
=={{header|Octave}}==
<
i = 2; j = 3;
while ( i <= length(v) )
Line 2,454:
endwhile
s = v;
endfunction</
<
disp(gnomesort(v));</
=={{header|ooRexx}}==
===version 1===
{{Trans|NetRexx}}
<
call demo
Line 2,557:
self~put(item, ix)
return
</syntaxhighlight>
'''Output:
<pre>
Line 2,585:
===version 2===
{{trans|REXX}}
<
* 28.06.2014 Walter Pachl
* 30.06.2014 corrected in sync with REXX version 2
Line 2,624:
Say 'element' right(i,2) x[i]
End
Return</
'''output'''
Similar to REXX version 2
Line 2,630:
=={{header|Oz}}==
{{trans|Haskell}}
<
fun {GnomeSort Xs}
case Xs of nil then nil
Line 2,646:
end
in
{Show {GnomeSort [3 1 4 1 5 9 2 6 5]}}</
=={{header|PARI/GP}}==
<
my(i=2,j=3,n=#v,t);
while(i<=n,
Line 2,663:
);
v
};</
=={{header|Pascal}}==
<
var
i, j, t : Integer;
Line 2,689:
end
end;
end;</
=={{header|Perl}}==
<
sub gnome_sort
Line 2,715:
}
return @a;
}</
<
print "$_\n" foreach gnome_sort( @arr );</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
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>
<!--</
=={{header|PHP}}==
<
$i = 1;
$j = 2;
Line 2,768:
}
$arr = array(3,1,6,2,9,4,7,8,5);
echo implode(',',gnomeSort($arr));</
<pre>1,2,3,4,5,6,7,8,9</pre>
=={{header|PicoLisp}}==
<
(let J (cdr Lst)
(for (I Lst (cdr I))
Line 2,781:
(setq I J J (cdr J))
(setq I (prior I Lst)) ) ) ) )
Lst )</
=={{header|PL/I}}==
<
DECLARE A(0:9) FIXED STATIC INITIAL (5, 2, 7, 1, 9, 8, 6, 3, 4, 0);
Line 2,811:
END GNOME_SORT;
END SORT;</
Results:
<pre>
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.
<
DIM i AS LONG, j AS LONG
i = 1
Line 2,854:
NEXT
CLOSE 1
END FUNCTION</
Sample output:
Line 2,861:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function gnomesort($a) {
$size, $i, $j = $a.Count, 1, 2
Line 2,882:
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11)
"$(gnomesort $array)"
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 2,889:
=={{header|PureBasic}}==
<
Protected Size = ArraySize(a()) + 1
Protected i = 1, j = 2
Line 2,907:
EndIf
Wend
EndProcedure</
=={{header|Python}}==
<
i,j,size = 1,2,len(a)
while i < size:
Line 2,924:
>>> gnomesort([3,4,2,5,1,6])
[1, 2, 3, 4, 5, 6]
>>></
=={{header|Quackery}}==
<
[ i^ 0 > if
[ dup i^ 1 - peek
Line 2,936:
swap i^ 1 - poke
-1 step ]
else 2drop ] ] ] is gnomesort ( [ --> [ )</
=={{header|R}}==
===Starting from 1===
<
{
i <- 1
Line 2,965:
x
}
gnomesort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782</
===Starting from 2===
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.
<
{
i <- 2
Line 2,999:
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)
strings <- c("We", "hold", "these", "truths", "to", "be", "self-evident", "that", "all", "men", "are", "created", "equal")</
{{out}}
Line 3,011:
=={{header|Racket}}==
<
#lang racket
Line 3,046:
[else `(,ps ,n ,p . ,ns)]))]))]))
(gnome-sort2 '(3 2 1 4 5 6) <=)
</syntaxhighlight>
=={{header|Raku}}==
Line 3,052:
{{Works with|rakudo|2016.03}}
<syntaxhighlight lang="raku"
my ($i, $j) = 1, 2;
while $i < @a {
Line 3,064:
}
}
}</
my @n = (1..10).roll(20);
Line 3,070:
=={{header|Rascal}}==
<
public list[int] gnomeSort(a){
Line 3,088:
j += 1;}}}
return a;
}</
An example output:
<
list[int]: [-31,0,1,2,4,65,83,99,782]</
=={{header|REXX}}==
===version 1===
This REXX version uses an unity-based array (instead of a zero-based array).
<
call gen /*generate the @ stemmed array. */
call show 'before sort' /*display the before array elements.*/
Line 3,118:
end /*j*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say ' element' right(j, w) arg(1)":" @.j; end; return</
{{out|output|:}}
Line 3,145:
===version 2===
<
* 28.06.2014 Walter Pachl cf ooRexx version 2
* only style changes (reformatting) and adapt for ooRexx compatibility
Line 3,191:
End
Return
</syntaxhighlight>
'''output'''
<pre>before sort
Line 3,215:
=={{header|Ring}}==
<
aList = [ 5, 6, 1, 2, 9, 14, 15, 7, 8, 97]
gnomeSort(aList)
Line 3,237:
i = j
j = j + 1 ok ok end
</syntaxhighlight>
=={{header|Ruby}}==
<
def gnomesort!
i, j = 1, 2
Line 3,259:
ary = [7,6,5,9,8,4,3,1,2,0]
ary.gnomesort!
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</
=={{header|Rust}}==
<
let len = a.len();
let mut i: usize = 1;
Line 3,287:
gnome_sort(&mut v);
println!("after: {:?}", v);
}</
{{out}}
Line 3,296:
=={{header|Scala}}==
<
def gnomeSort(a: Array[Int]): Unit = {
var (i, j) = (1, 2)
Line 3,308:
}
}
}</
=={{header|Scheme}}==
<
; arguments are in order or equal.
(define (gnome-sort-compar in-order input-list)
Line 3,331:
(gnome
(cdr p) ; Prev list shorter by one
(cons next-pot (cons prev-pot (cdr n))))))))))</
#;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)
=={{header|Sidef}}==
<
method gnomesort {
var (i=1, j=2);
Line 3,356:
var ary = [7,6,5,9,8,4,3,1,2,0];
say ary.gnomesort;</
{{out}}
<pre>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</pre>
Line 3,362:
=={{header|Smalltalk}}==
<
"Utility"
Line 3,394:
]
]
].</
'''Testing'''
<
r := Random new.
o := OrderedCollection new.
Line 3,406:
gnomesort value: o.
1 to: 10 do: [ :i | (o at: i) displayNl ].</
=={{header|SNOBOL4}}==
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.
<
* GNOME(V) -- gnome sort of the numerical vector V.
*** HELPER FUNCTIONS ***********************************************************
Line 3,474:
prototype(v) ',' :S(FRETURN)F(RETURN)
****************************************
GNOME.END</
The test script looks like this:
<
GNOME.TEST output = 'valid values...'
Line 3,505:
output = 'PASSED!'
END</
=={{header|Swift}}==
<
var i = 1
var j = 2
Line 3,529:
print("before: \(array)")
gnomeSort(&array)
print(" after: \(array)")</
{{out}}
Line 3,539:
=={{header|Tcl}}==
{{tcllib|struct::list}}
<
package require struct::list
Line 3,562:
}
puts [gnomesort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</
=={{header|TI-83 BASIC}}==
Line 3,597:
=={{header|True BASIC}}==
{{trans|IS-BASIC}}
<
RANDOMIZE !RAMDOMZE TIMER en QBASIC
DIM array(-5 TO 12)
Line 3,641:
NEXT i
END SUB
</syntaxhighlight>
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">PRINT "Gnome sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
Line 3,697:
PRINT
RETURN</
=={{header|Ursala}}==
Line 3,704:
finding an item out of order, bubble it backwards to its
proper position and resume scanning forward from where it was.
<
@NiX ^=lx -+ # iteration
~&r?\~& @lNXrX ->llx2rhPlrPCTxPrtPX~&lltPlhPrCXPrX ~&ll&& @llPrXh not "p", # backward bubble
->~&rhPlCrtPX ~&r&& ~&lZ!| @bh "p"+- # forward scan</
Most of the code is wasted on dull but unavoidable stack manipulations.
Here is a test program using the lexical partial order relation.
<
#cast %s
t = gnome_sort(lleq) 'dfijhkwlawfkjnksdjnoqwjefgflkdsgioi'</
output:
<pre>
Line 3,721:
=={{header|VBA}}==
{{trans|Phix}}<
Dim i As Integer: i = 1
Dim j As Integer: j = 2
Line 3,745:
Public Sub main()
Debug.Print Join(gnomeSort([{5, 3, 1, 7, 4, 1, 1, 20}]), ", ")
End Sub</
<pre>1, 1, 1, 3, 4, 5, 7, 20</pre>
=={{header|VBScript}}==
====Implementation====
<
dim i
dim j
Line 3,776:
x = y
y = temp
end sub</
====Invocation====
<
dim b
Line 3,799:
a = array( now(), now()-1,now()+2)
b = gnomeSort( a )
wscript.echo join(a, ", ")</
====Output====
<
23, 89, 234, 345, 456, 456, 547, 567, 567, 568, 649, 2345, 5769
True, True, True, True, False, False, False, False
-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</
Note: All data in VBScript is of type Variant. Thus the code can sort many different types of data without code modification.
=={{header|Vlang}}==
{{trans|go}}
<
mut a := [170, 45, 75, -90, -802, 24, 2, 66]
println("before: $a")
Line 3,830:
j++
}
}</
{{out}}
<pre>before: [170, 45, 75, -90, -802, 24, 2, 66]
Line 3,837:
=={{header|Wren}}==
<
var size = a.count
var i = 1
Line 3,869:
System.print()
}
}</
{{out}}
Line 3,891:
=={{header|XPL0}}==
<
proc GnomeSort(A(0..Size-1)); \Sort array A
Line 3,918:
GnomeSort(A, 10);
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )];
]</
{{out}}
Line 3,926:
=={{header|zkl}}==
<
i,j,size := 1,2,a.len();
while(i < size){
Line 3,938:
}//while
a
}</
<
{{out}}
<pre>L(" "," "," ","T","a","e","h","i","i","s","s","s","t","t")</pre>
|