Download presentation
Presentation is loading. Please wait.
1
Match-and-Stop Search Will find FIRST match Use Boolean variable to denote whether a match has been found or not Found initially False If a match is found, we set it to True Dim Found As Boolean Found = False POS = 0 SearchValue = Inputbox… Do While (Found=false and POS<(Array size)) POS = POS +1 If Array(POS) = SearchValue Then Found=True End If Loop If Found= True then Print success at location POS Else Print Failure End If
2
Match-and-Stop Search Read an array of 75 runner names and array of 75 runner times from a file and search for a specific name input through an inputbox If found, display rank and time Else, display not found What would happen if we didn’t use POS<(Array size) ? What would happen if the else is included in the loop and not outside
3
Sequential Search (2) Exhaustive search (must check all array elements) E.g. find all values less than a given value, maximum, or minimum Must scan whole array We use a For Loop For Pos = 1 to (CTR) If condition is true then do action
4
Bubble sort is one of the simple algorithms to sort data Goes through a list several times (passes) On every pass, bubbles largest number to end of array compares a number with its right neighbor If the number is larger, it is swapped/exchanged with its neighbor Larger numbers are said to “bubble down” to their proper places in the list After every pass, we place a number in its proper position so the number of comparisons made is decreased by 1 Bubble Sort
5
Algorithm: sort N numbers Input numbers to be sorted into an array of size N Make N-1 passes through the array Compare each number with its right neighbor and swap them if they are out of order Print sorted arrays Note that on pass 1, we make N-1 comparisons Pass 2, we make N-2 comparisons … Pass x, we make N-x comparisons How do we swap two values X and Y?
6
Swapping in Bubble Sort List(I) = List(I+1) List(I+1) = List(I) ???? Lost List(I)! We need to save it in a temporary variable before storing List(I+1) in it Temp = List(I) List(I) = List(I+1) List(I+1) = Temp
7
Bubble Sort Example Private Sub Sortbutton_Click() Dim MyNumbers(1 to 100) As Integer Dim Pos As Integer, Pass As Integer, CTR As Integer, Temp As Integer, MyNumber As Integer, Comp As Integer Open App.Path & "\Numbers.txt" for Input As #1 CTR = 0 Do Until EOF(1) CTR = CTR + 1 Input #1,MyNumbers(Count) Loop Close #1 ‘ print all array numbers (before sorting) on same line Results.Print "The unsorted list is:" For Pos = 1 To CTR picResult.Print MyNumbers(Pos), Next Pos picResult.Print ‘to go to a new line
8
Bubble Sort For Pass = 1 To CTR-1 For Pos= 1 To (CTR – Pass) If MyNumbers(Pos) > MyNumbers(Pos +1) Then Temp = MyNumbers(Pos) MyNumbers(Pos) = MyNumbers(Pos +1) MyNumbers(Pos +1) = Temp End If Next Pos Next Pass Results.Print "The sorted list becomes:" For Pos = 1 To CTR Results.Print MyNumbers(Pos); Next Pos End Sub
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.