Sorting algorithms/Gnome sort: Difference between revisions
Content added Content deleted
(added Easylang) |
(Grouping BASIC dialects) |
||
Line 793: | Line 793: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BBC BASIC}}=== |
|||
{{works with|QuickBasic|4.5}} |
|||
<syntaxhighlight lang="bbcbasic">DEF PROC_GnomeSort1(Size%) |
|||
I%=2 |
|||
J%=2 |
|||
REPEAT |
|||
IF data%(J%-1) <=data%(J%) THEN |
|||
I%+=1 |
|||
J%=I% |
|||
ELSE |
|||
SWAP data%(J%-1),data%(J%) |
|||
J%-=1 |
|||
IF J%=1 THEN |
|||
I%+=1 |
|||
J%=I% |
|||
ENDIF |
|||
ENDIF |
|||
UNTIL I%>Size% |
|||
ENDPROC</syntaxhighlight> |
|||
==={{header|FreeBASIC}}=== |
|||
Used the task pseudo code as a base |
|||
<syntaxhighlight lang="freebasic">' version 21-10-2016 |
|||
' compile with: fbc -s console |
|||
' for boundry checks on array's compile with: fbc -s console -exx |
|||
Sub gnomesort(gnome() As Long) |
|||
' sort from lower bound to the highter bound |
|||
' array's can have subscript range from -2147483648 to +2147483647 |
|||
Dim As Long lb = LBound(gnome) |
|||
Dim As Long ub = UBound(gnome) |
|||
Dim As Long i = lb +1, j = lb +2 |
|||
While i < (ub +1) |
|||
' replace "<=" with ">=" for downwards sort |
|||
If gnome(i -1) <= gnome(i) Then |
|||
i = j |
|||
j += 1 |
|||
Else |
|||
Swap gnome(i -1), gnome(i) |
|||
i -= 1 |
|||
If i = lb Then |
|||
i = j |
|||
j += 1 |
|||
End If |
|||
End If |
|||
Wend |
|||
End Sub |
|||
' ------=< MAIN >=------ |
|||
Dim As Long i, array(-7 To 7) |
|||
Dim As Long a = LBound(array), b = UBound(array) |
|||
Randomize Timer |
|||
For i = a To b : array(i) = i : Next |
|||
For i = a To b ' little shuffle |
|||
Swap array(i), array(Int(Rnd * (b - a +1)) + a) |
|||
Next |
|||
Print "unsort "; |
|||
For i = a To b : Print Using "####"; array(i); : Next : Print |
|||
gnomesort(array()) ' sort the array |
|||
Print " sort "; |
|||
For i = a To b : Print Using "####"; array(i); : Next : Print |
|||
' empty keyboard buffer |
|||
While Inkey <> "" : Wend |
|||
Print : Print "hit any key to end program" |
|||
Sleep |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<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> |
|||
==={{header|Gambas}}=== |
|||
'''[https://gambas-playground.proko.eu/?gist=d91a871bd9f43cd9644c89baa3ee861a Click this link to run this code]''' |
|||
<syntaxhighlight 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</syntaxhighlight> |
|||
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|IS-BASIC}}=== |
|||
<syntaxhighlight lang="is-basic"> |
|||
100 PROGRAM "GnomeSrt.bas" |
|||
110 RANDOMIZE |
|||
120 NUMERIC ARRAY(-5 TO 12) |
|||
130 CALL INIT(ARRAY) |
|||
140 CALL WRITE(ARRAY) |
|||
150 CALL GNOMESORT(ARRAY) |
|||
160 CALL WRITE(ARRAY) |
|||
170 DEF INIT(REF A) |
|||
180 FOR I=LBOUND(A) TO UBOUND(A) |
|||
190 LET A(I)=RND(98)+1 |
|||
200 NEXT |
|||
210 END DEF |
|||
220 DEF WRITE(REF A) |
|||
230 FOR I=LBOUND(A) TO UBOUND(A) |
|||
240 PRINT A(I); |
|||
250 NEXT |
|||
260 PRINT |
|||
270 END DEF |
|||
280 DEF GNOMESORT(REF A) |
|||
290 LET I=LBOUND(A)+1:LET J=I+1 |
|||
300 DO WHILE I<=UBOUND(A) |
|||
310 IF A(I-1)<=A(I) THEN |
|||
320 LET I=J:LET J=J+1 |
|||
330 ELSE |
|||
340 LET T=A(I-1):LET A(I-1)=A(I):LET A(I)=T |
|||
350 LET I=I-1 |
|||
360 IF I=LBOUND(A) THEN LET I=J:LET J=J+1 |
|||
370 END IF |
|||
380 LOOP |
|||
390 END DEF</syntaxhighlight> |
|||
==={{header|PowerBASIC}}=== |
|||
The [[#BASIC|BASIC]] example will work as-is if the array is declared in the same function as the sort. This example doesn't require that, but forces you to know your data type beforehand. |
|||
<syntaxhighlight lang="powerbasic">SUB gnomeSort (a() AS LONG) |
|||
DIM i AS LONG, j AS LONG |
|||
i = 1 |
|||
j = 2 |
|||
WHILE (i < UBOUND(a) + 1) |
|||
IF (a(i - 1) <= a(i)) THEN |
|||
i = j |
|||
INCR j |
|||
ELSE |
|||
SWAP a(i - 1), a(i) |
|||
DECR i |
|||
IF 0 = i THEN |
|||
i = j |
|||
INCR j |
|||
END IF |
|||
END IF |
|||
WEND |
|||
END SUB |
|||
FUNCTION PBMAIN () AS LONG |
|||
DIM n(9) AS LONG, x AS LONG |
|||
RANDOMIZE TIMER |
|||
OPEN "output.txt" FOR OUTPUT AS 1 |
|||
FOR x = 0 TO 9 |
|||
n(x) = INT(RND * 9999) |
|||
PRINT #1, n(x); ","; |
|||
NEXT |
|||
PRINT #1, |
|||
gnomeSort n() |
|||
FOR x = 0 TO 9 |
|||
PRINT #1, n(x); ","; |
|||
NEXT |
|||
CLOSE 1 |
|||
END FUNCTION</syntaxhighlight> |
|||
Sample output: |
|||
7426 , 7887 , 8297 , 2671 , 7586 , 7160 , 1195 , 665 , 9352 , 6199 , |
|||
665 , 1195 , 2671 , 6199 , 7160 , 7426 , 7586 , 7887 , 8297 , 9352 , |
|||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic">Procedure GnomeSort(Array a(1)) |
|||
Protected Size = ArraySize(a()) + 1 |
|||
Protected i = 1, j = 2 |
|||
While i < Size |
|||
If a(i - 1) <= a(i) |
|||
;for descending SORT, use >= for comparison |
|||
i = j |
|||
j + 1 |
|||
Else |
|||
Swap a(i - 1), a(i) |
|||
i - 1 |
|||
If i = 0 |
|||
i = j |
|||
j + 1 |
|||
EndIf |
|||
EndIf |
|||
Wend |
|||
EndProcedure</syntaxhighlight> |
|||
==={{header|QuickBasic}}=== |
|||
{{works with|QuickBasic|4.5}} |
|||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang="qbasic">dim a(0 to n-1) as integer |
<syntaxhighlight lang="qbasic">dim a(0 to n-1) as integer |
||
Line 815: | Line 1,032: | ||
end if |
end if |
||
wend</syntaxhighlight> |
wend</syntaxhighlight> |
||
==={{header|TI-83 BASIC}}=== |
|||
Store input into L<sub>1</sub>, run prgmSORTGNOM, output will be in L<sub>2</sub>. |
|||
:1→P |
|||
:L<sub>1</sub>→L<sub>2</sub> |
|||
:While P<dim(L<sub>2</sub>) |
|||
:If PP=1 |
|||
:Then |
|||
:P+1→P |
|||
:Else |
|||
:If L<sub>2</sub>(P)≥L<sub>2</sub>(P-1) |
|||
:Then |
|||
:P+1→P |
|||
:Else |
|||
:L<sub>2</sub>(P)→Q |
|||
:L<sub>2</sub>(P-1)→L<sub>2</sub>(P) |
|||
:Q→L<sub>2</sub>(P-1) |
|||
:P-1→P |
|||
:End |
|||
:End |
|||
:End |
|||
:If L<sub>2</sub>(dim(L<sub>2</sub>))<L<sub>2</sub>(dim(L<sub>2</sub>)-1) |
|||
:Then |
|||
:L<sub>2</sub>(dim(L<sub>2</sub>))→Q |
|||
:L<sub>2</sub>(dim(L<sub>2</sub>)-1)→L<sub>2</sub>(dim(L<sub>2</sub>)) |
|||
:Q→L<sub>2</sub>(dim(L<sub>2</sub>)-1) |
|||
:End |
|||
:DelVar P |
|||
:DelVar Q |
|||
:Return |
|||
==={{header|True BASIC}}=== |
|||
{{trans|IS-BASIC}} |
|||
<syntaxhighlight lang="qbasic">RANDOMIZE !RAMDOMIZE TIMER en QBASIC |
|||
DIM array(-5 TO 12) |
|||
CALL iniciarray(array()) |
|||
PRINT "unsort: "; |
|||
CALL escritura(array()) |
|||
CALL gnomeSort(array()) |
|||
PRINT |
|||
PRINT " sort: "; |
|||
CALL escritura(array()) |
|||
END |
|||
SUB escritura (array()) |
|||
FOR i = LBOUND(array) TO UBOUND(array) |
|||
PRINT array(i); |
|||
NEXT i |
|||
PRINT |
|||
END SUB |
|||
SUB gnomeSort (array()) |
|||
LET i = LBOUND(array) + 1 |
|||
LET j = i + 1 |
|||
DO WHILE i <= UBOUND(array) |
|||
IF array(i - 1) <= array(i) THEN |
|||
LET i = j |
|||
LET j = j + 1 |
|||
ELSE |
|||
LET T = array(i - 1) |
|||
LET array(i - 1) = array(i) |
|||
LET array(i) = T |
|||
LET i = i - 1 |
|||
IF i = LBOUND(array) THEN |
|||
LET i = j |
|||
LET j = j + 1 |
|||
END IF |
|||
END IF |
|||
LOOP |
|||
END SUB |
|||
SUB iniciarray (array()) |
|||
FOR i = LBOUND(array) TO UBOUND(array) |
|||
LET array(i) = (RND * 98) + 1 |
|||
NEXT i |
|||
END SUB</syntaxhighlight> |
|||
==={{header|uBasic/4tH}}=== |
|||
<syntaxhighlight lang="text">PRINT "Gnome sort:" |
|||
n = FUNC (_InitArray) |
|||
PROC _ShowArray (n) |
|||
PROC _Gnomesort (n) |
|||
PROC _ShowArray (n) |
|||
PRINT |
|||
END |
|||
_Gnomesort PARAM (1) ' Gnome sort |
|||
LOCAL (2) |
|||
b@=1 |
|||
c@=2 |
|||
DO WHILE b@ < a@ |
|||
IF @(b@-1) > @(b@) THEN |
|||
PROC _Swap (b@, b@-1) |
|||
b@ = b@ - 1 |
|||
IF b@ THEN |
|||
CONTINUE |
|||
ENDIF |
|||
ENDIF |
|||
b@ = c@ |
|||
c@ = c@ + 1 |
|||
LOOP |
|||
RETURN |
|||
_Swap PARAM(2) ' Swap two array elements |
|||
PUSH @(a@) |
|||
@(a@) = @(b@) |
|||
@(b@) = POP() |
|||
RETURN |
|||
_InitArray ' Init example array |
|||
PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 |
|||
FOR i = 0 TO 9 |
|||
@(i) = POP() |
|||
NEXT |
|||
RETURN (i) |
|||
_ShowArray PARAM (1) ' Show array subroutine |
|||
FOR i = 0 TO a@-1 |
|||
PRINT @(i), |
|||
NEXT |
|||
PRINT |
|||
RETURN</syntaxhighlight> |
|||
==={{header|VBA}}=== |
|||
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function gnomeSort(s As Variant) As Variant |
|||
Dim i As Integer: i = 1 |
|||
Dim j As Integer: j = 2 |
|||
Dim tmp As Integer |
|||
Do While i < UBound(s) |
|||
If s(i) <= s(i + 1) Then |
|||
i = j |
|||
j = j + 1 |
|||
Else |
|||
tmp = s(i) |
|||
s(i) = s(i + 1) |
|||
s(i + 1) = tmp |
|||
i = i - 1 |
|||
If i = 0 Then |
|||
i = j |
|||
j = j + 1 |
|||
End If |
|||
End If |
|||
Loop |
|||
gnomeSort = s |
|||
End Function |
|||
Public Sub main() |
|||
Debug.Print Join(gnomeSort([{5, 3, 1, 7, 4, 1, 1, 20}]), ", ") |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre>1, 1, 1, 3, 4, 5, 7, 20</pre> |
|||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
Line 879: | Line 1,255: | ||
ENDLOCAL |
ENDLOCAL |
||
EXIT /B 0</syntaxhighlight> |
EXIT /B 0</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
|||
<syntaxhighlight lang="bbcbasic">DEF PROC_GnomeSort1(Size%) |
|||
I%=2 |
|||
J%=2 |
|||
REPEAT |
|||
IF data%(J%-1) <=data%(J%) THEN |
|||
I%+=1 |
|||
J%=I% |
|||
ELSE |
|||
SWAP data%(J%-1),data%(J%) |
|||
J%-=1 |
|||
IF J%=1 THEN |
|||
I%+=1 |
|||
J%=I% |
|||
ENDIF |
|||
ENDIF |
|||
UNTIL I%>Size% |
|||
ENDPROC</syntaxhighlight> |
|||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
Line 1,283: | Line 1,640: | ||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
<syntaxhighlight lang="eiffel"> |
||
class |
class |
||
Line 1,347: | Line 1,703: | ||
end |
end |
||
end</syntaxhighlight> |
|||
end |
|||
</syntaxhighlight> |
|||
Test: |
Test: |
||
<syntaxhighlight lang="eiffel"> |
<syntaxhighlight lang="eiffel"> |
||
Line 1,384: | Line 1,738: | ||
gnome: GNOME_SORT [INTEGER] |
gnome: GNOME_SORT [INTEGER] |
||
end</syntaxhighlight> |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,463: | Line 1,815: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang">-module(gnome_sort). |
<syntaxhighlight lang="erlang">-module(gnome_sort). |
||
-export([gnome/1]). |
-export([gnome/1]). |
||
Line 1,548: | Line 1,899: | ||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
<syntaxhighlight lang="fantom"> |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
Line 1,727: | Line 2,077: | ||
end program example</syntaxhighlight> |
end program example</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
|||
Used the task pseudo code as a base |
|||
<syntaxhighlight lang="freebasic">' version 21-10-2016 |
|||
' compile with: fbc -s console |
|||
' for boundry checks on array's compile with: fbc -s console -exx |
|||
Sub gnomesort(gnome() As Long) |
|||
' sort from lower bound to the highter bound |
|||
' array's can have subscript range from -2147483648 to +2147483647 |
|||
Dim As Long lb = LBound(gnome) |
|||
Dim As Long ub = UBound(gnome) |
|||
Dim As Long i = lb +1, j = lb +2 |
|||
While i < (ub +1) |
|||
' replace "<=" with ">=" for downwards sort |
|||
If gnome(i -1) <= gnome(i) Then |
|||
i = j |
|||
j += 1 |
|||
Else |
|||
Swap gnome(i -1), gnome(i) |
|||
i -= 1 |
|||
If i = lb Then |
|||
i = j |
|||
j += 1 |
|||
End If |
|||
End If |
|||
Wend |
|||
End Sub |
|||
' ------=< MAIN >=------ |
|||
Dim As Long i, array(-7 To 7) |
|||
Dim As Long a = LBound(array), b = UBound(array) |
|||
Randomize Timer |
|||
For i = a To b : array(i) = i : Next |
|||
For i = a To b ' little shuffle |
|||
Swap array(i), array(Int(Rnd * (b - a +1)) + a) |
|||
Next |
|||
Print "unsort "; |
|||
For i = a To b : Print Using "####"; array(i); : Next : Print |
|||
gnomesort(array()) ' sort the array |
|||
Print " sort "; |
|||
For i = a To b : Print Using "####"; array(i); : Next : Print |
|||
' empty keyboard buffer |
|||
While Inkey <> "" : Wend |
|||
Print : Print "hit any key to end program" |
|||
Sleep |
|||
End</syntaxhighlight> |
|||
{{out}} |
|||
<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> |
|||
=={{header|Gambas}}== |
|||
'''[https://gambas-playground.proko.eu/?gist=d91a871bd9f43cd9644c89baa3ee861a Click this link to run this code]''' |
|||
<syntaxhighlight 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</syntaxhighlight> |
|||
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}}== |
||
Line 2,041: | Line 2,287: | ||
lst := list(5, -1, -4, 2, 9) |
lst := list(5, -1, -4, 2, 9) |
||
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</syntaxhighlight> |
lst gnomeSortInPlace println # ==> list(-4, -1, 2, 5, 9)</syntaxhighlight> |
||
=={{header|IS-BASIC}}== |
|||
<syntaxhighlight lang="is-basic"> |
|||
100 PROGRAM "GnomeSrt.bas" |
|||
110 RANDOMIZE |
|||
120 NUMERIC ARRAY(-5 TO 12) |
|||
130 CALL INIT(ARRAY) |
|||
140 CALL WRITE(ARRAY) |
|||
150 CALL GNOMESORT(ARRAY) |
|||
160 CALL WRITE(ARRAY) |
|||
170 DEF INIT(REF A) |
|||
180 FOR I=LBOUND(A) TO UBOUND(A) |
|||
190 LET A(I)=RND(98)+1 |
|||
200 NEXT |
|||
210 END DEF |
|||
220 DEF WRITE(REF A) |
|||
230 FOR I=LBOUND(A) TO UBOUND(A) |
|||
240 PRINT A(I); |
|||
250 NEXT |
|||
260 PRINT |
|||
270 END DEF |
|||
280 DEF GNOMESORT(REF A) |
|||
290 LET I=LBOUND(A)+1:LET J=I+1 |
|||
300 DO WHILE I<=UBOUND(A) |
|||
310 IF A(I-1)<=A(I) THEN |
|||
320 LET I=J:LET J=J+1 |
|||
330 ELSE |
|||
340 LET T=A(I-1):LET A(I-1)=A(I):LET A(I)=T |
|||
350 LET I=I-1 |
|||
360 IF I=LBOUND(A) THEN LET I=J:LET J=J+1 |
|||
370 END IF |
|||
380 LOOP |
|||
390 END DEF</syntaxhighlight> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Line 2,921: | Line 3,134: | ||
0 1 2 3 4 5 6 7 8 9 |
0 1 2 3 4 5 6 7 8 9 |
||
</pre> |
</pre> |
||
=={{header|PowerBASIC}}== |
|||
The [[#BASIC|BASIC]] example will work as-is if the array is declared in the same function as the sort. This example doesn't require that, but forces you to know your data type beforehand. |
|||
<syntaxhighlight lang="powerbasic">SUB gnomeSort (a() AS LONG) |
|||
DIM i AS LONG, j AS LONG |
|||
i = 1 |
|||
j = 2 |
|||
WHILE (i < UBOUND(a) + 1) |
|||
IF (a(i - 1) <= a(i)) THEN |
|||
i = j |
|||
INCR j |
|||
ELSE |
|||
SWAP a(i - 1), a(i) |
|||
DECR i |
|||
IF 0 = i THEN |
|||
i = j |
|||
INCR j |
|||
END IF |
|||
END IF |
|||
WEND |
|||
END SUB |
|||
FUNCTION PBMAIN () AS LONG |
|||
DIM n(9) AS LONG, x AS LONG |
|||
RANDOMIZE TIMER |
|||
OPEN "output.txt" FOR OUTPUT AS 1 |
|||
FOR x = 0 TO 9 |
|||
n(x) = INT(RND * 9999) |
|||
PRINT #1, n(x); ","; |
|||
NEXT |
|||
PRINT #1, |
|||
gnomeSort n() |
|||
FOR x = 0 TO 9 |
|||
PRINT #1, n(x); ","; |
|||
NEXT |
|||
CLOSE 1 |
|||
END FUNCTION</syntaxhighlight> |
|||
Sample output: |
|||
7426 , 7887 , 8297 , 2671 , 7586 , 7160 , 1195 , 665 , 9352 , 6199 , |
|||
665 , 1195 , 2671 , 6199 , 7160 , 7426 , 7586 , 7887 , 8297 , 9352 , |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
<syntaxhighlight lang="powershell">function gnomesort($a) { |
||
function gnomesort($a) { |
|||
$size, $i, $j = $a.Count, 1, 2 |
$size, $i, $j = $a.Count, 1, 2 |
||
while($i -lt $size) { |
while($i -lt $size) { |
||
Line 2,986: | Line 3,155: | ||
} |
} |
||
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11) |
$array = @(60, 21, 19, 36, 63, 8, 100, 80, 3, 87, 11) |
||
"$(gnomesort $array)" |
"$(gnomesort $array)"</syntaxhighlight> |
||
</syntaxhighlight> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
3 8 11 19 21 36 60 63 80 87 100 |
3 8 11 19 21 36 60 63 80 87 100 |
||
</pre> |
</pre> |
||
=={{header|PureBasic}}== |
|||
<syntaxhighlight lang="purebasic">Procedure GnomeSort(Array a(1)) |
|||
Protected Size = ArraySize(a()) + 1 |
|||
Protected i = 1, j = 2 |
|||
While i < Size |
|||
If a(i - 1) <= a(i) |
|||
;for descending SORT, use >= for comparison |
|||
i = j |
|||
j + 1 |
|||
Else |
|||
Swap a(i - 1), a(i) |
|||
i - 1 |
|||
If i = 0 |
|||
i = j |
|||
j + 1 |
|||
EndIf |
|||
EndIf |
|||
Wend |
|||
EndProcedure</syntaxhighlight> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 3,466: | Line 3,613: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
<syntaxhighlight lang="smalltalk">Smalltalk at: #gnomesort put: nil. |
<syntaxhighlight lang="smalltalk">Smalltalk at: #gnomesort put: nil. |
||
Line 3,514: | Line 3,660: | ||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Implementation of the Gnome sort. Note this is an overengineered approach that performs many checks the real world would need but might obfuscate intent. As such the actual implementation is carefully labelled and the rest can be ignored except as interest dictates. |
Implementation of the Gnome sort. Note this is an overengineered approach that performs many checks the real world would need but might obfuscate intent. As such the actual implementation is carefully labelled and the rest can be ignored except as interest dictates. |
||
Line 3,668: | Line 3,813: | ||
puts [gnomesort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</syntaxhighlight> |
puts [gnomesort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
|||
Store input into L<sub>1</sub>, run prgmSORTGNOM, output will be in L<sub>2</sub>. |
|||
:1→P |
|||
:L<sub>1</sub>→L<sub>2</sub> |
|||
:While P<dim(L<sub>2</sub>) |
|||
:If PP=1 |
|||
:Then |
|||
:P+1→P |
|||
:Else |
|||
:If L<sub>2</sub>(P)≥L<sub>2</sub>(P-1) |
|||
:Then |
|||
:P+1→P |
|||
:Else |
|||
:L<sub>2</sub>(P)→Q |
|||
:L<sub>2</sub>(P-1)→L<sub>2</sub>(P) |
|||
:Q→L<sub>2</sub>(P-1) |
|||
:P-1→P |
|||
:End |
|||
:End |
|||
:End |
|||
:If L<sub>2</sub>(dim(L<sub>2</sub>))<L<sub>2</sub>(dim(L<sub>2</sub>)-1) |
|||
:Then |
|||
:L<sub>2</sub>(dim(L<sub>2</sub>))→Q |
|||
:L<sub>2</sub>(dim(L<sub>2</sub>)-1)→L<sub>2</sub>(dim(L<sub>2</sub>)) |
|||
:Q→L<sub>2</sub>(dim(L<sub>2</sub>)-1) |
|||
:End |
|||
:DelVar P |
|||
:DelVar Q |
|||
:Return |
|||
=={{header|True BASIC}}== |
|||
{{trans|IS-BASIC}} |
|||
<syntaxhighlight lang="qbasic"> |
|||
RANDOMIZE !RAMDOMZE TIMER en QBASIC |
|||
DIM array(-5 TO 12) |
|||
CALL iniciarray(array()) |
|||
PRINT "unsort: "; |
|||
CALL escritura(array()) |
|||
CALL gnomeSort(array()) |
|||
PRINT |
|||
PRINT " sort: "; |
|||
CALL escritura(array()) |
|||
END |
|||
SUB escritura (array()) |
|||
FOR i = LBOUND(array) TO UBOUND(array) |
|||
PRINT array(i); |
|||
NEXT i |
|||
PRINT |
|||
END SUB |
|||
SUB gnomeSort (array()) |
|||
LET i = LBOUND(array) + 1 |
|||
LET j = i + 1 |
|||
DO WHILE i <= UBOUND(array) |
|||
IF array(i - 1) <= array(i) THEN |
|||
LET i = j |
|||
LET j = j + 1 |
|||
ELSE |
|||
LET T = array(i - 1) |
|||
LET array(i - 1) = array(i) |
|||
LET array(i) = T |
|||
LET i = i - 1 |
|||
IF i = LBOUND(array) THEN |
|||
LET i = j |
|||
LET j = j + 1 |
|||
END IF |
|||
END IF |
|||
LOOP |
|||
END SUB |
|||
SUB iniciarray (array()) |
|||
FOR i = LBOUND(array) TO UBOUND(array) |
|||
LET array(i) = (RND * 98) + 1 |
|||
NEXT i |
|||
END SUB |
|||
</syntaxhighlight> |
|||
=={{header|uBasic/4tH}}== |
|||
<syntaxhighlight lang="text">PRINT "Gnome sort:" |
|||
n = FUNC (_InitArray) |
|||
PROC _ShowArray (n) |
|||
PROC _Gnomesort (n) |
|||
PROC _ShowArray (n) |
|||
PRINT |
|||
END |
|||
_Gnomesort PARAM (1) ' Gnome sort |
|||
LOCAL (2) |
|||
b@=1 |
|||
c@=2 |
|||
DO WHILE b@ < a@ |
|||
IF @(b@-1) > @(b@) THEN |
|||
PROC _Swap (b@, b@-1) |
|||
b@ = b@ - 1 |
|||
IF b@ THEN |
|||
CONTINUE |
|||
ENDIF |
|||
ENDIF |
|||
b@ = c@ |
|||
c@ = c@ + 1 |
|||
LOOP |
|||
RETURN |
|||
_Swap PARAM(2) ' Swap two array elements |
|||
PUSH @(a@) |
|||
@(a@) = @(b@) |
|||
@(b@) = POP() |
|||
RETURN |
|||
_InitArray ' Init example array |
|||
PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 |
|||
FOR i = 0 TO 9 |
|||
@(i) = POP() |
|||
NEXT |
|||
RETURN (i) |
|||
_ShowArray PARAM (1) ' Show array subroutine |
|||
FOR i = 0 TO a@-1 |
|||
PRINT @(i), |
|||
NEXT |
|||
PRINT |
|||
RETURN</syntaxhighlight> |
|||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 3,824: | Line 3,834: | ||
'adddeffffgghiiijjjjkkkkllnnooqsswww' |
'adddeffffgghiiijjjjkkkkllnnooqsswww' |
||
</pre> |
</pre> |
||
=={{header|VBA}}== |
|||
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function gnomeSort(s As Variant) As Variant |
|||
Dim i As Integer: i = 1 |
|||
Dim j As Integer: j = 2 |
|||
Dim tmp As Integer |
|||
Do While i < UBound(s) |
|||
If s(i) <= s(i + 1) Then |
|||
i = j |
|||
j = j + 1 |
|||
Else |
|||
tmp = s(i) |
|||
s(i) = s(i + 1) |
|||
s(i + 1) = tmp |
|||
i = i - 1 |
|||
If i = 0 Then |
|||
i = j |
|||
j = j + 1 |
|||
End If |
|||
End If |
|||
Loop |
|||
gnomeSort = s |
|||
End Function |
|||
Public Sub main() |
|||
Debug.Print Join(gnomeSort([{5, 3, 1, 7, 4, 1, 1, 20}]), ", ") |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre>1, 1, 1, 3, 4, 5, 7, 20</pre> |
|||
=={{header|VBScript}}== |
=={{header|VBScript}}== |