Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
Deadmarshal (talk | contribs) |
|||
Line 1,535: | Line 1,535: | ||
1486 ; 9488 ; 9894 ; 17479 ; 18989 ; 23119 ; 23233 ; 24927 ; 25386 ; 26689 ; |
1486 ; 9488 ; 9894 ; 17479 ; 18989 ; 23119 ; 23233 ; 24927 ; 25386 ; 26689 ; |
||
</pre> |
</pre> |
||
=={{header|GWBASIC}}== |
|||
{{works with|BASICA}} |
|||
{{works with|QBASIC/QUICKBASIC}} |
|||
{{works with|VBDOS}} |
|||
Sorts N integers in an array a() with the Insertion sort |
|||
<syntaxhighlight lang="qbasic"> |
|||
10 'SAVE "INSERTGW",A |
|||
20 DEFINT A-Z |
|||
30 OPTION BASE 1 |
|||
40 N=20: R=100: I=0: Y=0: V=0: P=0 |
|||
50 DIM A(N) |
|||
60 ' Creates the disordered array |
|||
70 CLS: PRINT "This program sorts by Insertion a list of randomly generated numbers." |
|||
80 PRINT: PRINT "Unsorted list:" |
|||
90 RANDOMIZE TIMER |
|||
100 FOR I = 1 TO N |
|||
110 A(I) = INT(RND * R) + 1 |
|||
120 NEXT I |
|||
130 GOSUB 260 |
|||
140 PRINT: PRINT "Sorted list." |
|||
150 ' Insertion Sort |
|||
160 FOR I=1 TO N |
|||
170 V=A(I): P=I-1: S=1 |
|||
180 WHILE P>0 AND S=1 |
|||
185 S=0 |
|||
190 IF A(P) > V THEN A(P+1)=A(P): P=P-1: S=1 |
|||
200 WEND |
|||
210 A(P+1) = V |
|||
220 NEXT I |
|||
230 GOSUB 260 |
|||
240 PRINT: PRINT "End of program execution." |
|||
250 END |
|||
260 ' Print list routine |
|||
270 FOR I=1 TO N |
|||
280 PRINT A(I); |
|||
290 NEXT I |
|||
300 PRINT |
|||
310 RETURN |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
This program sorts by Insertion a list of randomly generated numbers. |
|||
Unsorted list: |
|||
73 11 100 68 28 48 3 36 15 34 31 26 47 61 5 58 15 86 69 79 |
|||
Sorted list: |
|||
3 5 11 15 15 26 28 31 34 36 47 48 58 61 68 69 73 79 86 100 |
|||
End of program execution. |
|||
</pre> |
|||
==={{header|ZX BASIC}}=== |
==={{header|ZX BASIC}}=== |