Sorting algorithms/Insertion sort: Difference between revisions
Sorting algorithms/Insertion sort (view source)
Revision as of 19:47, 9 November 2009
, 14 years agomoved REALbasic to BASIC; changed RB markup to VB; added QBasic
No edit summary |
(moved REALbasic to BASIC; changed RB markup to VB; added QBasic) |
||
Line 116:
}
}</lang>
{{works with|QBasic}}
This version (which is a translation of the REALbasic version immediately below) should work on any BASIC that can accept arrays as function arguments.
DIM n(10) AS INTEGER, L AS INTEGER, o AS STRING
FOR L = 0 TO 10
n(L) = INT(RND * 32768)
NEXT
InsertionSort n()
FOR L = 1 TO 10
PRINT n(L); ";";
NEXT
SUB InsertionSort (theList() AS INTEGER)
FOR insertionElementIndex = 1 TO UBOUND(theList)
j = insertionElementIndex - 1
DO WHILE (j >= 0)
'necessary for BASICs without short-circuit evaluation
IF (insertionElement < theList(j)) THEN
theList(j + 1) = theList(j)▼
j = j - 1
ELSE
EXIT DO
END IF
LOOP
theList(j + 1) = insertionElement▼
Sample output:
1486 ; 9488 ; 9894 ; 17479 ; 18989 ; 23119 ; 23233 ; 24927 ; 25386 ; 26689 ;
{{works with|REALbasic}}
<lang vb> Sub InsertionSort(theList() as Integer)
for insertionElementIndex as Integer = 1 to UBound(theList)▼
dim insertionElement as Integer = theList(insertionElementIndex)
dim j as Integer = insertionElementIndex - 1
while (j >= 0) and (insertionElement < theList(j))
theList(j + 1) = theList(j)
j = j - 1
wend
theList(j + 1) = insertionElement
next▼
End Sub</lang>
=={{header|C}}==
Line 459 ⟶ 510:
insertionsort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782
</lang>
▲=={{header|REALbasic}}==
▲<lang realbasic> Sub InsertionSort(theList() as Integer)
▲ for insertionElementIndex as Integer = 1 to UBound(theList)
▲ dim insertionElement as Integer = theList(insertionElementIndex)
▲ dim j as Integer = insertionElementIndex - 1
▲ while (j >= 0) and (insertionElement < theList(j))
▲ theList(j + 1) = theList(j)
▲ j = j - 1
▲ wend
▲ theList(j + 1) = insertionElement
▲ next
▲ End Sub</lang>
=={{header|Ruby}}==
|