Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(Emacs Lisp: Make code more idiomatic) |
(added ZX BASIC insertion sort Subroutine) |
||
Line 1,062: | Line 1,062: | ||
1486 ; 9488 ; 9894 ; 17479 ; 18989 ; 23119 ; 23233 ; 24927 ; 25386 ; 26689 ; |
1486 ; 9488 ; 9894 ; 17479 ; 18989 ; 23119 ; 23233 ; 24927 ; 25386 ; 26689 ; |
||
</pre> |
</pre> |
||
==={{header|ZX BASIC}}=== |
|||
Sorts N elements in array i() into ascending order. Invoke with GO SUB 500. |
|||
<lang zxbasic>500 FOR j=1 TO N-1 |
|||
510 IF i(j)<=i(j+1) THEN NEXT j: RETURN |
|||
520 LET c=i(j+1) |
|||
530 FOR k=j TO 1 STEP -1: IF i(k)>c THEN LET i(k+1)=i(k): NEXT k |
|||
540 LET i(k+1)=c |
|||
600 NEXT j: RETURN</lang> |
|||
For those who prefer GO TOs over conditional NEXTs (fine in ZX BASIC but problematic for compilers and stack-dependent interpreters like NextBASIC’s integer extensions) replace NEXT J: RETURN in line 510 with GO TO 600 and use this line 530: |
|||
<pre> |
|||
530 IF k>0 THEN IF i(k)>c THEN LET i(k+1)=i(k): LET k=k-1: GO TO 530 </pre> |
|||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |