Code: (Select All)
_Title "Insert sorting algorithm demostration"
Rem Insert sort demonstration
Rem it creates an ordered increasing subset of elements taken from the scrambled set of items to be ordered
Rem it uses 2 indexes: 1 for ordererd subset and 1 for unordered subset
Randomize Timer
Const max = 10
Dim a(1 To max) As Integer, b(1 To max) As Integer
Print " Original array unordered";
init a()
Show a()
Print String$(80, "@");
CopyArr a(), b()
Show a()
Print "ordering array...from left decreasing "
InsertFromLeft a()
Color 4
Show a()
Color 7
CopyArr b(), a()
Show a()
Print "ordering array...from left increasing"
InsertFromLeftInc a()
Color 2
Show a()
Color 7
CopyArr b(), a()
Show a()
Print "ordering array...from right increasing"
InsertFromRight a()
Color 5
Show a()
Color 7
CopyArr b(), a()
Show a()
Print "ordering array...from right decreasing"
InsertFromRightDec a()
Color 3
Show a()
Color 7
End
Rem -----INSERTION SORT starting from leftest item of array-----------
Rem it orders a() starting from the left of array in decreasing way
Rem the array is ordered from left/higher value to right/lower value
Rem -------------------------------------------------------------------
Sub InsertFromLeft (A() As Integer)
For b = 1 To (max - 1) Step 1
c = b
Do While c > 0
If A(c + 1) > A(c) Then
Swap A(c), A(c + 1)
End If
c = c - 1
Loop
Next
End Sub
Rem -----INSERTION SORT starting from leftest item of array-----------
Rem it orders a() starting from the left of array in increasing way
Rem the array is ordered from left/higher value to right/lower value
Rem -------------------------------------------------------------------
Sub InsertFromLeftInc (A() As Integer)
For b = 1 To (max - 1) Step 1
c = b
Do While c > 0
If A(c + 1) < A(c) Then
Swap A(c), A(c + 1)
End If
c = c - 1
Loop
Next
End Sub
Rem -----INSERTION SORT starting from rightest item of array-----------
Rem it orders a() starting from the right of array in increasing way
Rem the array is ordered from left/higher value to right/lower value
Rem -------------------------------------------------------------------
Sub InsertFromRight (A() As Integer)
For b = (max - 1) To 1 Step -1
c = b
While c < (max)
If A(c + 1) < A(c) Then Swap A(c), A(c + 1)
c = c + 1
Wend
Next
End Sub
Rem -----INSERTION SORT starting from rightest item of array-----------
Rem it orders a() starting from the right of array in increasing way
Rem the array is ordered from left/higher value to right/lower value
Rem -------------------------------------------------------------------
Sub InsertFromRightDec (A() As Integer)
For b = (max - 1) To 1 Step -1
c = b
While c < (max)
If A(c + 1) > A(c) Then Swap A(c), A(c + 1)
c = c + 1
Wend
Next
End Sub
Rem -----INIT--------------------------------------------------
Rem it initializes array a() giving it random values
Rem -----------------------------------------------------------
Sub init (a() As Integer)
For b = 1 To max
a(b) = Int(Rnd * max - 1) + 1
Next b
Print
End Sub
Rem -----SHOW--------------------------------------------------
Rem it print out the items of a()
Rem it must be aware that a() has almost 1 item (max = 1)
Rem -----------------------------------------------------------
Sub Show (a() As Integer)
For b = 1 To max
Print a(b); "_";
Next b
Print
End Sub
Rem -----COPYARR-----------------------------------------------
Rem it copies a() in b ()
Rem it must be aware that b() has equal or more items then a()
Rem -----------------------------------------------------------
Sub CopyArr (a() As Integer, b() As Integer)
For b = 1 To max
b(b) = a(b)
Next b
Print " Array copied!"
End Sub