Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
No edit summary |
Simple9371 (talk | contribs) (Add VBScript) |
||
Line 3,098: | Line 3,098: | ||
<45,58,69,74,82,82,88,89,104,112> |
<45,58,69,74,82,82,88,89,104,112> |
||
</pre> |
</pre> |
||
=={{header|VBScript}}== |
|||
{{trans|BASIC}} |
|||
<lang vb>Randomize |
|||
Dim n(9) 'nine is the upperbound. |
|||
'since VBS arrays are 0-based, it will have 10 elements. |
|||
For L = 0 to 9 |
|||
n(L) = Int(Rnd * 32768) |
|||
Next |
|||
WScript.StdOut.Write "ORIGINAL : " |
|||
For L = 0 to 9 |
|||
WScript.StdOut.Write n(L) & ";" |
|||
Next |
|||
InsertionSort n |
|||
WScript.StdOut.Write vbCrLf & " SORTED : " |
|||
For L = 0 to 9 |
|||
WScript.StdOut.Write n(L) & ";" |
|||
Next |
|||
'the function |
|||
Sub InsertionSort(theList) |
|||
For insertionElementIndex = 1 To UBound(theList) |
|||
insertionElement = theList(insertionElementIndex) |
|||
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 |
|||
Next |
|||
End Sub |
|||
</lang> |
|||
{{Out}} |
|||
<pre>ORIGINAL : 26699;2643;10249;31612;21346;19702;29799;31115;20413;5197; |
|||
SORTED : 2643;5197;10249;19702;20413;21346;26699;29799;31115;31612;</pre> |
|||
=={{header|XPL0}}== |
=={{header|XPL0}}== |