Sorting algorithms/Gnome sort: Difference between revisions
Content added Content deleted
m (added a ;Task (bold) section header, added whitespace before the TOC.) |
No edit summary |
||
Line 1,074: | Line 1,074: | ||
<pre>unsort 4 -5 5 1 -3 -1 -2 -6 0 7 -4 6 2 -7 3 |
<pre>unsort 4 -5 5 1 -3 -1 -2 -6 0 7 -4 6 2 -7 3 |
||
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre> |
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre> |
||
=={{header|Gambas}}== |
|||
'''[https://gambas-playground.proko.eu/?gist=d91a871bd9f43cd9644c89baa3ee861a Click this link to run this code]''' |
|||
<lang gambas>Public Sub Main() |
|||
Dim siCount As Short |
|||
Dim siCounti As Short = 1 |
|||
Dim siCountj As Short = 2 |
|||
Dim siToSort As Short[] = [249, 28, 111, 36, 171, 98, 29, 448, 44, 147, 154, 46, 102, 183, 24] |
|||
Print "To sort: - "; |
|||
GoSub Display |
|||
While siCounti < siToSort.Count |
|||
If siToSort[siCounti - 1] <= siToSort[siCounti] Then |
|||
siCounti = siCountj |
|||
Inc siCountj |
|||
Else |
|||
Swap siToSort[siCounti - 1], siToSort[siCounti] |
|||
Dec siCounti |
|||
If siCounti = 0 Then |
|||
siCounti = siCountj |
|||
Inc siCountj |
|||
Endif |
|||
Endif |
|||
Wend |
|||
Print "Sorted: - "; |
|||
GoSub Display |
|||
Return |
|||
'-------------------------------------------- |
|||
Display: |
|||
For siCount = 0 To siToSort.Max |
|||
Print Format(Str(siToSort[siCount]), "####"); |
|||
If siCount <> siToSort.max Then Print ","; |
|||
Next |
|||
Print |
|||
Return |
|||
End</lang> |
|||
Output: |
|||
<pre> |
|||
To sort: - 249, 28, 111, 36, 171, 98, 29, 448, 44, 147, 154, 46, 102, 183, 24 |
|||
Sorted: - 24, 28, 29, 36, 44, 46, 98, 102, 111, 147, 154, 171, 183, 249, 448 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |