Sorting algorithms/Gnome sort: Difference between revisions
Content added Content deleted
(Added Rust solution) |
m (→version 1: changed whitespace and indentations, aligned statements better.) |
||
Line 2,474: | Line 2,474: | ||
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). |
||
<lang rexx>/*REXX program sorts an array using the gnome sort algorithm (elements contain blanks). */ |
<lang rexx>/*REXX program sorts an array using the gnome sort algorithm (elements contain blanks). */ |
||
call |
call gen /*generate the @ stemmed array. */ |
||
call |
call show 'before sort' /*display the before array elements.*/ |
||
say copies('▒', 60) /*show a separator line between sorts. */ |
|||
call |
call gnomeSort # /*invoke the well─known gnome sort. */ |
||
call |
call show ' after sort' /*display the after array elements.*/ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 2,486: | Line 2,486: | ||
do #=1 while @.#\==''; end; #= #-1; w= length(#); return /*get #items*/ |
do #=1 while @.#\==''; end; #= #-1; w= length(#); return /*get #items*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
gnomeSort: procedure expose @.; parse arg n; k= 2 |
gnomeSort: procedure expose @.; parse arg n; k= 2 /*N: is number items. */ |
||
do j=3 while k<=n; p= k - 1 /*P: is previous item.*/ |
|||
if @.p<<=@.k then do; k= j; iterate; end /*order is OK so far. */ |
|||
_= @.p; @.p= @.k; @.k= _ /*swap two @ entries. */ |
|||
k= k-1; if k==1 then k= j; else j= j-1 /*test for 1st index. */ |
|||
end /*j*/; return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
show: do j=1 for #; say ' element' right(j, w) arg(1)":" @.j; end; |
show: do j=1 for #; say ' element' right(j, w) arg(1)":" @.j; end; return</lang> |
||
{{out|output|:}} |
{{out|output|:}} |
||
<pre> |
<pre> |