Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
m (moved realbasic back to its own section; minor markup edits to rexx)
Line 138: Line 138:


=={{header|BASIC}}==
=={{header|BASIC}}==
{{trans|REALbasic}}
{{works with|QBasic}}
{{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.
This version should work on any BASIC that can accept arrays as function arguments.
<lang qbasic>DECLARE SUB InsertionSort (theList() AS INTEGER)
<lang qbasic>DECLARE SUB InsertionSort (theList() AS INTEGER)


Line 174: Line 175:
Sample output:
Sample output:
1486 ; 9488 ; 9894 ; 17479 ; 18989 ; 23119 ; 23233 ; 24927 ; 25386 ; 26689 ;
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|BBC BASIC}}==
=={{header|BBC BASIC}}==
Line 963: Line 951:
insertionsort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782</lang>
insertionsort(c(4, 65, 2, -31, 0, 99, 83, 782, 1)) # -31 0 1 2 4 65 83 99 782</lang>


=={{header|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|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program sorts an array using the insertion-sort method. */
<lang rexx>
/*REXX program sorts an array using the insertion-sort method. */


call gen@ /*generate array elements. */
call gen@ /*generate array elements. */
Line 1,021: Line 1,020:


say copies('─',80) /*show a seperator line. */
say copies('─',80) /*show a seperator line. */
return
return</lang>

</lang>
Output:
Output:
element 1 before sort: ---Monday's Child Is Fair of Face (by Mother Goose)---
<pre style="height:30ex;overflow:scroll">
element 1 before sort: ---Monday's Child Is Fair of Face (by Mother Goose)---
element 2 before sort: Monday's child is fair of face;
element 2 before sort: Monday's child is fair of face;
element 3 before sort: Tuesday's child is full of grace;
element 3 before sort: Tuesday's child is full of grace;
element 4 before sort: Wednesday's child is full of woe;
element 4 before sort: Wednesday's child is full of woe;
element 5 before sort: Thursday's child has far to go;
element 5 before sort: Thursday's child has far to go;
element 6 before sort: Friday's child is loving and giving;
element 6 before sort: Friday's child is loving and giving;
element 7 before sort: Saturday's child works hard for a living;
element 7 before sort: Saturday's child works hard for a living;
element 8 before sort: But the child that is born on the Sabbath day
element 8 before sort: But the child that is born on the Sabbath day
element 9 before sort: Is blithe and bonny, good and gay.
────────────────────────────────────────────────────────────────────────────────
element 9 before sort: Is blithe and bonny, good and gay.
────────────────────────────────────────────────────────────────────────────────
element 1 after sort: ---Monday's Child Is Fair of Face (by Mother Goose)---

element 1 after sort: ---Monday's Child Is Fair of Face (by Mother Goose)---
element 2 after sort: But the child that is born on the Sabbath day
element 2 after sort: But the child that is born on the Sabbath day
element 3 after sort: Friday's child is loving and giving;
element 3 after sort: Friday's child is loving and giving;
element 4 after sort: Is blithe and bonny, good and gay.
element 4 after sort: Is blithe and bonny, good and gay.
element 5 after sort: Monday's child is fair of face;
element 5 after sort: Monday's child is fair of face;
element 6 after sort: Saturday's child works hard for a living;
element 6 after sort: Saturday's child works hard for a living;
element 7 after sort: Thursday's child has far to go;
element 7 after sort: Thursday's child has far to go;
element 8 after sort: Tuesday's child is full of grace;
element 8 after sort: Tuesday's child is full of grace;
element 9 after sort: Wednesday's child is full of woe;
────────────────────────────────────────────────────────────────────────────────
element 9 after sort: Wednesday's child is full of woe;
────────────────────────────────────────────────────────────────────────────────
</pre>


=={{header|Ruby}}==
=={{header|Ruby}}==