
Sorting Algorithm

This is a sorting algorithm.   It may be applied to a set of data in order to sort it.    
For comparing various sorts, see compare sorts.  
For other sorting algorithms,   see sorting algorithms,   or:

O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort




 This page uses content from Wikipedia. The original article was at Gnome sort. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



Gnome sort is a sorting algorithm which is similar to Insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in Bubble Sort.

The pseudocode for the algorithm is:

function gnomeSort(a[0..size-1])
    i := 1
    j := 2
    while i < size do
        if a[i-1] <= a[i] then
            // for descending sort, use >= for comparison
            i := j
            j := j + 1 
        else
            swap a[i-1] and a[i]
            i := i - 1
            if i = 0 then
                i := j
                j := j + 1
            endif
        endif
    done



Task

Implement the Gnome sort in your language to sort an array (or list) of numbers.




*        Gnome sort - Array base 0 - 15/04/2020
GNOME    CSECT
         USING  GNOME,R13          base register
         B      72(R15)            skip savearea
         DC     17F'0'             savearea
         SAVE   (14,12)            save previous context
         ST     R13,4(R15)         link backward
         ST     R15,8(R13)         link forward
         LR     R13,R15            set addressability
         LA     R6,1               i=1
         LA     R7,2               j=2
       DO WHILE=(C,R6,LT,NN)       while i<nn
         LR     R1,R6                i
         SLA    R1,2                 ~
         LA     R8,TT-4(R1)          @tt(i-1)
         LA     R9,TT(R1)            @tt(i)
         L      R2,0(R8)             tt(i-1)
       IF     C,R2,LE,0(R9) THEN     if tt(i-1)<=tt(i) then
         LR     R6,R7                  i=j
         LA     R7,1(R7)               j=j+1
       ELSE     ,                    else
         L      R4,0(R8)               t=tt(i-1)
         L      R3,0(R9)               tt(i)
         ST     R3,0(R8)               tt(i-1)=tt(i)
         ST     R4,0(R9)               tt(i)=t
         BCTR   R6,0                   i=i-1
       IF   LTR,R6,Z,R6 THEN           if i=0 then
         LR     R6,R7                    i=j
         LA     R7,1(R7)                 j=j+1
       ENDIF    ,                      endif
       ENDIF    ,                    endif
       ENDDO    ,                  endwhile
         LA     R10,PG             @buffer
         LA     R7,TT              @tt
         LA     R6,1               i=1
       DO WHILE=(C,R6,LE,NN)       do i=1 to nn
         L      R2,0(R7)             tt(i)
         XDECO  R2,XDEC              edit tt(i)
         MVC    0(5,R10),XDEC+7      output tt(i)
         LA     R10,5(R10)           @buffer
         LA     R7,4(R7)             @tt++
         LA     R6,1(R6)             i++
       ENDDO    ,                  enddo i
         XPRNT  PG,L'PG            print buffer
         L      R13,4(0,R13)       restore previous savearea pointer
         RETURN (14,12),RC=0       restore registers from calling save
TT       DC     F'557',F'5143',F'5432',F'6798',F'2874'
         DC     F'3499',F'6949',F'4992',F'2555',F'4175'
         DC     F'8264',F'3522',F'2045',F'2963',F'2625'
         DC     F'-764',F'1845',F'1485',F'5792',F'7562'
         DC     F'5044',F'3598',F'6817',F'4891',F'4350'
NN       DC     A((NN-TT)/L'TT)    number of items of tt
XDEC     DS     CL12               temp for xdeco
PG       DC     CL125' '           buffer
         REGEQU
         END    GNOME