There are several customers who like to use Basic4android for teaching purposes.
The following code can be useful for these purposes.
You should also check the Linked list tutorial.
Note that Basic4android is sold at half the price for academic usage. Contact erel@basic4ppc.com for more information.
Implementations of sorting algorithms are useful for teaching fundamental concepts such as algorithms analysis, complexity and data structures.
The following code includes implementations of the following sorting algorithms: bubble sort, selection sort, quick sort, merge sort and binary tree sort.
The code should be pretty straightforward once you understand the algorithm.
Code (file is also attached):
Code:
'Activity module Sub Process_Globals Type TreeElement(Value AsInt, Left As TreeElement, Right As TreeElement) End Sub
Sub Globals Dim lv AsListView Dim data(100) AsInt End Sub
Sub Activity_Create(FirstTime AsBoolean) lv.Initialize("lv") Activity.AddView(lv, 0, 0, 100%x, 100%y) lv.SingleLineLayout.Label.TextSize = 13 lv.SingleLineLayout.ItemHeight = 40dip FillWithRandomNumbers DisplayData Activity.AddMenuItem("Randomize", "mnuRandomize") Activity.AddMenuItem("Bubble Sort", "mnuBubbleSort") Activity.AddMenuItem("Quick Sort", "mnuQuickSort") Activity.AddMenuItem("Merge Sort", "mnuMergeSort") Activity.AddMenuItem("Selection Sort", "mnuSelectionSort") Activity.AddMenuItem("Binary Tree Sort", "mnuBinaryTreeSort") End Sub Sub lv_ItemClick (Position AsInt, Value AsObject) Activity.OpenMenu End Sub
Sub FillWithRandomNumbers For i = 0To data.Length - 1 data(i) = Rnd(0, 10000) Next End Sub
Sub DisplayData lv.Clear For i = 0To data.Length - 1 lv.AddSingleLine(data(i)) Next End Sub Sub mnuRandomize_Click FillWithRandomNumbers DisplayData End Sub
Sub mnuBubbleSort_Click Dim s AsLong s = DateTime.Now BubbleSort ToastMessageShow("Bubble Sort took: " & (DateTime.Now - s) & " ms", False) DisplayData End Sub
Sub mnuQuickSort_Click Dim s AsLong s = DateTime.Now QuickSort(0, data.Length - 1) ToastMessageShow("Quick Sort took: " & (DateTime.Now - s) & " ms", False) DisplayData End Sub
Sub mnuMergeSort_Click Dim s AsLong s = DateTime.Now data = MergeSort(data) ToastMessageShow("Merge Sort took: " & (DateTime.Now - s) & " ms", False) DisplayData End Sub
Sub mnuSelectionSort_Click Dim s AsLong s = DateTime.Now SelectionSort ToastMessageShow("Selection Sort took: " & (DateTime.Now - s) & " ms", False) DisplayData End Sub
Sub mnuBinaryTreeSort_Click Dim s AsLong s = DateTime.Now Dim root As TreeElement root.Initialize root.Value = data(0) For i = 1To data.Length - 1 InsertTreeElement(root, data(i)) Next TraverseTree(root, 0) ToastMessageShow("Binary Tree Sort took: " & (DateTime.Now - s) & " ms", False) DisplayData End Sub
Sub Swap(index1 AsInt, index2 AsInt) Dim temp AsInt temp = data(index1) data(index1) = data(index2) data(index2) = temp End Sub
Sub BubbleSort Dim swapped AsBoolean swapped = True DoWhile swapped swapped = False For i = 1To Data.Length - 1 If data(i - 1) > data(i) Then Swap(i-1, i) swapped = True EndIf Next Loop End Sub
Sub SelectionSort Dim minIndex AsInt For i = 0To data.Length - 1 minIndex = i For j = i + 1To data.Length - 1 If data(j) < data(minIndex) Then minIndex = j Next Swap(i, minIndex) Next End Sub
Sub QuickSort (left AsInt, right AsInt) If right > left Then Dim pivotIndex, pivotNewIndex AsInt pivotIndex = Rnd(left, right + 1) 'max value is exclusive pivotNewIndex = Partition(left, right, pivotIndex) QuickSort(left, pivotNewIndex - 1) QuickSort(pivotNewIndex + 1, right) EndIf End Sub
Sub Partition (left AsInt, right AsInt, pivotIndex AsInt) AsInt Dim pivotValue, storeIndex AsInt pivotValue = data(pivotIndex) Swap(pivotIndex, right) storeIndex = left For i = left To right - 1 If data(i) <= pivotValue Then Swap(i, storeIndex) storeIndex = storeIndex + 1 EndIf Next Swap(storeIndex, right) Return storeIndex End Sub
Sub MergeSort(arr() AsInt) AsInt() If arr.Length <= 1ThenReturn arr Dim middle AsInt middle = arr.Length / 2 Dim left(middle) AsInt Dim right(arr.Length - middle) AsInt For i = 0To middle - 1 left(i) = arr(i) Next For i = middle To arr.Length - 1 right(i - middle) = arr(i) Next left = MergeSort(left) right = MergeSort(right) Return Merge(left, right) End Sub
Sub Merge(left() AsInt, right() AsInt) AsInt() Dim leftIndex, rightIndex AsInt'initialized to 0 Dim result(left.Length + right.Length) AsInt For i = 0To result.Length - 1 If rightIndex = right.Length OR _ (leftIndex < left.Length AND left(leftIndex) < right(rightIndex)) Then result(i) = left(leftIndex) leftIndex = leftIndex + 1 Else result(i) = right(rightIndex) rightIndex = rightIndex + 1 EndIf Next Return result End Sub
Sub InsertTreeElement(Parent As TreeElement, Value AsInt) Dim leaf As TreeElement If Parent.IsInitialized = FalseThen Parent.Initialize Parent.Value = Value ElseIf Value < Parent.Value Then InsertTreeElement(Parent.Left, Value) Else InsertTreeElement(Parent.Right, Value) EndIf End Sub
Sub TraverseTree (Parent As TreeElement, ArrayIndex AsInt) AsInt If Parent.IsInitialized = FalseThenReturn ArrayIndex ArrayIndex = TraverseTree(Parent.Left, ArrayIndex) Data(ArrayIndex) = Parent.Value ArrayIndex = ArrayIndex + 1 ArrayIndex = TraverseTree(Parent.Right, ArrayIndex) Return ArrayIndex End Sub
If you want to dig WAY deeper on the sorting stuff... The bible used to be "Donald E. Knuth" Sorting and Searching Algorithms... Heavy reading if you are into that kind of stuff..
As a result of another thread, I looked at the Quick Sort routine in the code above and it doesn't look like QS routines I've used in the past, so I Googled Quick Sort and looked at some QS routines there and the tutorial routine doesn't look like what's going on in any of them either.
Private Sub QuickSort(C() AsString, ByVal First AsLong, ByVal Last AsLong) Dim Low AsLong, High AsLong Dim MidValue AsString
Low = First High = Last MidValue = C((First + Last) \ 2)
Do While C(Low) < MidValue Low = Low + 1 Wend
While C(High) > MidValue High = High - 1 Wend
If Low <= High Then Swap C(Low), C(High) Low = Low + 1 High = High - 1 EndIf LoopWhile Low <= High
If First < High Then QuickSort C, First, High If Low < Last Then QuickSort C, Low, Last End Sub
And this is the code in this tutorial:
Code:
Sub QuickSort (left AsInt, right AsInt) If right > left Then Dim pivotIndex, pivotNewIndex AsInt pivotIndex = Rnd(left, right + 1) 'max value is exclusive pivotNewIndex = Partition(left, right, pivotIndex) QuickSort(left, pivotNewIndex - 1) QuickSort(pivotNewIndex + 1, right) EndIf End Sub
Sub Partition (left AsInt, right AsInt, pivotIndex AsInt) AsInt Dim pivotValue, storeIndex AsInt pivotValue = data(pivotIndex) Swap(pivotIndex, right) storeIndex = left For i = left To right - 1 If data(i) <= pivotValue Then Swap(i, storeIndex) storeIndex = storeIndex + 1 EndIf Next Swap(storeIndex, right) Return storeIndex End Sub
In particular, the part of the code which says
Code:
While C(Low) < MidValue Low = Low + 1 Wend
While C(High) > MidValue High = High - 1 Wend
is common to every QS routine I've used or seen, but I can't figure how the tutorial code is accomplishing this, though this is very likely just my inability to figure out the tutorial code. So if you could straighten me out, I would appreciate it.