# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010
# 2# 2 Excel Sorting Data 1. select 2. choose the type of sort in the Data tab
# 3# 3 Sorting Algorithms Selection Sort Bubble Sort There are numerous ways to perform a sort. We will consider two: In the examples in this lecture we will sort arrays in ascending order but we could also have sorted in descending order.
# 4# 4 CS 105 Spring 2010 Selection Sort To sort a list in ascending order: Step 1 Swap the last element of the list with the element that contains the largest value. Step 2 Repeat Step 1 on the list with the last element removed until there is only one element remaining. See visualization of algorithm here.here
# 5# 5 CS 105 Spring 2010 Sub SortArray_selection() Dim strNames(1024) As String Dim intCount As Integer intCount = 0 ' Number of strings ' Read down column A until we find an empty cell Do While Cells(intCount + 1, 1).Value <> "" strNames(intCount + 1) = Cells(intCount + 1, 1).Value intCount = intCount + 1 Loop ' Call the SelectionSort sub (see code on next slide) SelectionSort strNames, intCount ' Display sorted strings in column B Do While intCount > 0 Cells(intCount, 2).Value = strNames(intCount) intCount = intCount - 1 Loop End Sub
# 6# 6 CS 105 Spring 2010 Sub SelectionSort(strNames() As String, intCount As Integer) Dim intIndex As Integer, intPosition As Integer, intFound as Integer Dim strTemp As String ' Find the largest (alphabetically) string to be placed in the last ' position in the array and then repeat to find the next largest For intPosition = intCount To 2 Step -1 ' start with a guess intFound = 1 ' check to see if our guess is the largest and if not then store ' both value and position of the larger value For intIndex = 2 To intPosition If strNames(intIndex) > strNames(intFound) Then intFound = intIndex End If Next intIndex ' swap the largest value with the value at intPosition strTemp = strNames(intPosition) strNames(intPosition) = strNames(intFound) strNames(intFound) = strTemp Next intPosition End Sub
# 7# 7 CS 105 Spring 2010 Bubble Sort To sort a list in ascending order: Step 1 starting with the first two elements in the list swap (if necessary) to make the second element the largest. Repeat with second and third elements and then third and fourth … until last element of the list is the largest. Step 2 Repeat Step 1 on the list with the largest element removed until there is only one element remaining. See visualization of algorithm here.here
# 8# 8 CS 105 Spring 2010 Sub SortArray_bubble() Dim strNames(1024) As String Dim intCount As Integer intCount = 0 ' Number of strings ' Read down column A until we find an empty cell Do While Cells(intCount + 1, 1).Value <> "" strNames(intCount + 1) = Cells(intCount + 1, 1).Value intCount = intCount + 1 Loop ' Call the BubbleSort sub (see code on next slide) BubbleSort strNames, intCount ' Display sorted strings in column B Do While intCount > 0 Cells(intCount, 2).Value = strNames(intCount) intCount = intCount - 1 Loop End Sub
# 9# 9 CS 105 Spring 2010 Sub BubbleSort(strNames() As String, intCount As Integer) Dim intIndex As Integer, intPosition As Integer Dim strTemp As String ' Find the largest (alphabetically) string to be ' placed in the last position in the array ' and then repeat to find the next largest For intPosition = intCount To 2 Step -1 ' compare two and swap (bubble up) the largest string ' if necessary For intIndex = 2 To intPosition If strNames(intIndex) < strNames(intIndex - 1) Then strTemp = strNames(intIndex) strNames(intIndex) = strNames(intIndex - 1) strNames(intIndex - 1) = strTemp End If Next intIndex Next intPosition End Sub
# 10 Best Sorting Algorithm? Time Counting number of comparisons (, =, …) Counting the number of swaps There are many ways to compare sorting algorithms Determining the best algorithm based on time is flawed since it is dependent on the particular hardware on which the test is performed. Counting the number of comparisons and the number of swaps is independent of the hardware.
# 11 CS 105 Spring 2010 An Improved Bubble Sort We would like to reduce the number of comparisons. One way to do this is that at each step we keep track of whether any swaps were made. If no swaps were made we know that the list had to be sorted. That is, we can stop our sort early! We will use a Boolean “flag” variable to keep track of any swaps that are made for each step.
# 12 CS 105 Spring 2010 Sub BubbleSort(strNames() As String, intCount As Integer) Dim intIndex As Integer, intPosition As Integer Dim strTemp As String Dim blnSwap As Boolean For intPosition = intCount To 2 Step -1 blnSwap = False For intIndex = 2 To intPosition If strNames(intIndex) < strNames(intIndex - 1) Then strTemp = strNames(intIndex) strNames(intIndex) = strNames(intIndex - 1) strNames(intIndex - 1) = strTemp blnSwap = True End If Next intIndex If blnSwap = False Then Exit Sub End If Next intPosition End Sub
# 13 Searching Algorithms Binary Search Sorting makes searching easier. There are numerous ways to perform a search. We will consider just one:
# 14 Searching a List Binary Search requires that the list be sorted. In this example the list is sorted in ascending order. To search for a target value in a list we Step 1 compare the target with the middle element in the list and if we find a match then we are done. Step 2 If the target is less(greater) than the middle value then we discard the right(left) half of the list. Step 3 Repeat Step 1--- applied to only the half of the list remaining from Step 2 until a match is found or the list is empty. CS 105 Spring 2010
# 15 CS 105 Spring 2010 Sub SearchArray_binarysearch() Dim strNames(1024) As String Dim strTarget As String Dim intCount As Integer, intIndex As Integer intCount = 0 ' Read values from column A into strNames Do While Cells(intCount + 1, 1).Value <> "" strNames(intCount + 1) = Cells(intCount + 1, 1).Value intCount = intCount + 1 Loop ' Sort the values in the strNames array BubbleSort strNames, intCount ' Ask the user for a name to search for strTarget = InputBox("Enter a name") ' Call BinarySearch to search the array strNames for strTarget intIndex = BinarySearch(strNames, intCount, strTarget) ' If intIndex = -1 then no match found If intIndex = -1 Then MsgBox "No match found for " & strTarget Else MsgBox "Match found at index = " & intIndex End If End Sub
# 16 CS 105 Spring 2010 Function BinarySearch(strNames() As String, intCount As Integer, strTarget As String) As Integer Dim intMidpt As Integer Dim intFirst As Integer, intLast As Integer BinarySearch = -1 ' BinarySearch = -1 means no match found intFirst = 1 intLast = intCount ' While array not empty (intFirst <= intLast) and no match (BinarySearch = -1) Do While (intFirst <= intLast) And (BinarySearch = -1) intMidpt = (intFirst + intLast) / 2 If strTarget > strNames(intMidpt) Then intFirst = intMidpt + 1 ElseIf strTarget < strNames(intMidpt) Then intLast = intMidpt - 1 Else BinarySearch = intMidpt End If Loop End Function