Sorting algorithms/Gnome sort: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 136:
abcdefghiijklmnopqrstuvwxyz
</pre>
 
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276379.html#276379 forum]
Line 467 ⟶ 468:
(decf position))
(t (incf position)))))</lang>
 
=={{header|D}}==
<lang d>import std.stdio, std.algorithm;
Line 749 ⟶ 751:
-7 -10 0 1 7 25 99
</pre>
 
=={{header|Elena}}==
ELENA 5.0 :
Line 1,022 ⟶ 1,025:
end program example</lang>
 
=={{header|FreeBASIC}}==
Used the task pseudo code as a base
Line 1,265 ⟶ 1,269:
Sorted Strings: [We,all,are,be,created,equal,hold,men,self-evident,that,these,to,truths]
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>procedure main() #: demonstrate various ways to sort a list and string
demosort(gnomesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
 
procedure gnomesort(X,op) #: return sorted list
local i,j
 
op := sortop(op,X) # select how and what we sort
 
j := (i := 2) + 1 # translation of pseudo code
while i <= *X do {
if op(X[i],X[i-1]) then {
X[i] :=: X[i -:= 1]
if i > 1 then next
}
j := (i := j) + 1
}
return X
end</lang>
 
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.
 
Abbreviated sample output:<pre>Sorting Demo using procedure gnomesort
on list : [ 3 14 1 5 9 2 6 3 ]
with op = &null: [ 1 2 3 3 5 6 9 14 ] (0 ms)
...
on string : "qwerty"
with op = &null: "eqrtwy" (0 ms)</pre>
 
=={{header|Io}}==
Line 1,305 ⟶ 1,339:
lst := list(5, -1, -4, 2, 9)
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>procedure main() #: demonstrate various ways to sort a list and string
demosort(gnomesort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
 
procedure gnomesort(X,op) #: return sorted list
local i,j
 
op := sortop(op,X) # select how and what we sort
 
j := (i := 2) + 1 # translation of pseudo code
while i <= *X do {
if op(X[i],X[i-1]) then {
X[i] :=: X[i -:= 1]
if i > 1 then next
}
j := (i := j) + 1
}
return X
end</lang>
 
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.
 
Abbreviated sample output:<pre>Sorting Demo using procedure gnomesort
on list : [ 3 14 1 5 9 2 6 3 ]
with op = &null: [ 1 2 3 3 5 6 9 14 ] (0 ms)
...
on string : "qwerty"
with op = &null: "eqrtwy" (0 ms)</pre>
 
=={{header|IS-BASIC}}==
Line 2,089 ⟶ 2,093:
<lang perl>my @arr = ( 10, 9, 8, 5, 2, 1, 1, 0, 50, -2 );
print "$_\n" foreach gnome_sort( @arr );</lang>
 
=={{header|Perl 6}}==
{{Works with|rakudo|2016.03}}
 
<lang perl6>sub gnome_sort (@a) {
my ($i, $j) = 1, 2;
while $i < @a {
if @a[$i - 1] <= @a[$i] {
($i, $j) = $j, $j + 1;
}
else {
(@a[$i - 1], @a[$i]) = @a[$i], @a[$i - 1];
$i--;
($i, $j) = $j, $j + 1 if $i == 0;
}
}
}</lang>
 
my @n = (1..10).roll(20);
say @n.&gnome_sort ~~ @n.sort ?? 'ok' !! 'not ok';
 
=={{header|Phix}}==
Line 2,166 ⟶ 2,150:
(setq I (prior I Lst)) ) ) ) )
Lst )</lang>
 
 
=={{header|PL/I}}==
Line 2,310 ⟶ 2,293:
[1, 2, 3, 4, 5, 6]
>>></lang>
 
=={{header|R}}==
<lang r>gnomesort <- function(x)
Line 2,376 ⟶ 2,359:
(gnome-sort2 '(3 2 1 4 5 6) <=)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.03}}
 
<lang perl6>sub gnome_sort (@a) {
my ($i, $j) = 1, 2;
while $i < @a {
if @a[$i - 1] <= @a[$i] {
($i, $j) = $j, $j + 1;
}
else {
(@a[$i - 1], @a[$i]) = @a[$i], @a[$i - 1];
$i--;
($i, $j) = $j, $j + 1 if $i == 0;
}
}
}</lang>
 
my @n = (1..10).roll(20);
say @n.&gnome_sort ~~ @n.sort ?? 'ok' !! 'not ok';
 
=={{header|Rascal}}==
Line 2,582 ⟶ 2,586:
}
}</lang>
 
=={{header|Scheme}}==
<lang scheme>; supply comparison function, which returns true if first and second
Line 2,791 ⟶ 2,796:
PRINT
RETURN</lang>
 
=={{header|Ursala}}==
The function is parameterized by a relational predicate and
Line 2,811 ⟶ 2,817:
'adddeffffgghiiijjjjkkkkllnnooqsswww'
</pre>
 
=={{header|VBA}}==
{{trans|Phix}}<lang vb>Private Function gnomeSort(s As Variant) As Variant
Line 2,838 ⟶ 2,845:
End Sub</lang>{{out}}
<pre>1, 1, 1, 3, 4, 5, 7, 20</pre>
 
=={{header|VBScript}}==
====Implementation====
10,333

edits