Sorting algorithms/Gnome sort: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 136: | Line 136: | ||
abcdefghiijklmnopqrstuvwxyz |
abcdefghiijklmnopqrstuvwxyz |
||
</pre> |
</pre> |
||
=={{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] |
||
Line 467: | Line 468: | ||
(decf position)) |
(decf position)) |
||
(t (incf position)))))</lang> |
(t (incf position)))))</lang> |
||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.algorithm; |
<lang d>import std.stdio, std.algorithm; |
||
Line 749: | Line 751: | ||
-7 -10 0 1 7 25 99 |
-7 -10 0 1 7 25 99 |
||
</pre> |
</pre> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
Line 1,022: | Line 1,025: | ||
end program example</lang> |
end program example</lang> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Used the task pseudo code as a base |
Used the task pseudo code as a base |
||
Line 1,265: | Line 1,269: | ||
Sorted Strings: [We,all,are,be,created,equal,hold,men,self-evident,that,these,to,truths] |
Sorted Strings: [We,all,are,be,created,equal,hold,men,self-evident,that,these,to,truths] |
||
</pre> |
</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}}== |
=={{header|Io}}== |
||
Line 1,305: | Line 1,339: | ||
lst := list(5, -1, -4, 2, 9) |
lst := list(5, -1, -4, 2, 9) |
||
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</lang> |
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}}== |
=={{header|IS-BASIC}}== |
||
Line 2,089: | Line 2,093: | ||
<lang perl>my @arr = ( 10, 9, 8, 5, 2, 1, 1, 0, 50, -2 ); |
<lang perl>my @arr = ( 10, 9, 8, 5, 2, 1, 1, 0, 50, -2 ); |
||
print "$_\n" foreach gnome_sort( @arr );</lang> |
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}}== |
=={{header|Phix}}== |
||
Line 2,166: | Line 2,150: | ||
(setq I (prior I Lst)) ) ) ) ) |
(setq I (prior I Lst)) ) ) ) ) |
||
Lst )</lang> |
Lst )</lang> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
Line 2,310: | Line 2,293: | ||
[1, 2, 3, 4, 5, 6] |
[1, 2, 3, 4, 5, 6] |
||
>>></lang> |
>>></lang> |
||
=={{header|R}}== |
=={{header|R}}== |
||
<lang r>gnomesort <- function(x) |
<lang r>gnomesort <- function(x) |
||
Line 2,376: | Line 2,359: | ||
(gnome-sort2 '(3 2 1 4 5 6) <=) |
(gnome-sort2 '(3 2 1 4 5 6) <=) |
||
</lang> |
</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}}== |
=={{header|Rascal}}== |
||
Line 2,582: | Line 2,586: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<lang scheme>; supply comparison function, which returns true if first and second |
<lang scheme>; supply comparison function, which returns true if first and second |
||
Line 2,791: | Line 2,796: | ||
PRINT |
PRINT |
||
RETURN</lang> |
RETURN</lang> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
The function is parameterized by a relational predicate and |
The function is parameterized by a relational predicate and |
||
Line 2,811: | Line 2,817: | ||
'adddeffffgghiiijjjjkkkkllnnooqsswww' |
'adddeffffgghiiijjjjkkkkllnnooqsswww' |
||
</pre> |
</pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}<lang vb>Private Function gnomeSort(s As Variant) As Variant |
{{trans|Phix}}<lang vb>Private Function gnomeSort(s As Variant) As Variant |
||
Line 2,838: | Line 2,845: | ||
End Sub</lang>{{out}} |
End Sub</lang>{{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==== |