Sorting algorithms/Insertion sort: Difference between revisions

moved 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>
 
=={{header|REALbasicBASIC}}==
{{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.
<lang realbasicqbasic>DECLARE SubSUB InsertionSort (theList() asAS IntegerINTEGER)
 
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)
dim j as Integer =DIM insertionElementIndex -AS 1INTEGER
FOR insertionElementIndex = 1 TO UBOUND(theList)
while (j >= 0) andDIM (insertionElement <AS theList(j))INTEGER
dim insertionElement as Integer insertionElement = theList(insertionElementIndex)
j = DIM j -AS 1INTEGER
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
wendNEXT
EndEND SubSUB</lang>
 
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}}==
1,150

edits