Jump to content

Sorting algorithms/Insertion sort: Difference between revisions

m
moved realbasic back to its own section; minor markup edits to rexx
m (moved realbasic back to its own section; minor markup edits to rexx)
Line 138:
 
=={{header|BASIC}}==
{{works withtrans|REALbasic}}
{{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 qbasic>DECLARE SUB InsertionSort (theList() AS INTEGER)
 
Line 174 ⟶ 175:
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|BBC BASIC}}==
Line 963 ⟶ 951:
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}}==
<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. */
Line 1,021 ⟶ 1,020:
 
say copies('─',80) /*show a seperator line. */
return</lang>
 
</lang>
Output:
element 1 before sort: ---Monday's Child Is Fair of Face (by Mother Goose)---
<pre style="height:30ex;overflow:scroll">
element 12 before sort: ---Monday's Childchild Isis Fairfair of Face (by Mother Goose)---face;
element 23 before sort: MondayTuesday's child is fairfull of facegrace;
element 34 before sort: TuesdayWednesday's child is full of gracewoe;
element 45 before sort: WednesdayThursday's child ishas fullfar ofto woego;
element 56 before sort: ThursdayFriday's child hasis farloving toand gogiving;
element 67 before sort: FridaySaturday's child isworks lovinghard andfor givinga living;
element 78 before sort: Saturday'sBut the child worksthat is born hardon forthe aSabbath living;day
element 89 before sort: ButIs theblithe childand thatbonny, isgood bornand on the Sabbath daygay.
────────────────────────────────────────────────────────────────────────────────
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 12 after sort: ---Monday'sBut Childthe Ischild Fairthat ofis Faceborn on (bythe MotherSabbath Goose)---day
element 23 after sort: But theFriday's child that is born on theloving Sabbathand daygiving;
element 34 after sort: Friday'sIs childblithe isand bonny, lovinggood and giving;gay.
element 45 after sort: IsMonday's blithechild andis bonny,fair goodof and gay.face;
element 56 after sort: MondaySaturday's child isworks fairhard offor facea living;
element 67 after sort: SaturdayThursday's child workshas hardfar forto a livinggo;
element 78 after sort: ThursdayTuesday's child hasis farfull toof gograce;
element 89 after sort: TuesdayWednesday's child is full of gracewoe;
────────────────────────────────────────────────────────────────────────────────
element 9 after sort: Wednesday's child is full of woe;
────────────────────────────────────────────────────────────────────────────────
</pre>
 
=={{header|Ruby}}==
1,150

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.