Jump to content

Sorting algorithms/Insertion sort: Difference between revisions

BBC BASIC solution updated
(→‎{{header|REXX}}: changed DO loop indentations, changed comments, added comments, changed fence, re-order subroutines in alphbetical order. -- ~~~~)
(BBC BASIC solution updated)
Line 191:
 
=={{header|BBC BASIC}}==
Note that the array index is assumed to start at zero.
<lang BBCBASIC>DEF PROC_InsertionSort(Size%)
<lang bbcbasic> DIM test(9)
FOR I%=2 TO Size%
test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
Temp%=data%(I%)
PROCinsertionsort(test(), 10)
J%=I%
FOR i% = 0 TO 9
WHILE J%>1 AND Temp%<data%(J%-1)
data%(J%)=data% PRINT test(Ji%-1) ;
J%=J%-1NEXT
ENDWHILE PRINT
END
IF J% <> I% data%(J%)=Temp%
NEXT I%
DEF PROCinsertionsort(a(), n%)
ENDPROC</lang>
LOCAL i%, j%, t
FOR Ii% =2 1 TO Sizen%-1
t = a(i%)
j% = i%
WHILE Jj%>10 AND Temp%t<data%a(ABS(Jj%-1))
a(j%) = a(j%-1)
j% -= 1
ENDWHILE
a(j%) = t
NEXT
ENDPROC</lang>
'''Output:'''
<pre>
-31 0 1 2 2 4 65 83 99 782
</pre>
 
=={{header|C}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.