# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS...

16
# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010

Transcript of # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS...

Page 1: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 1

Searching andSortingSearching andSorting

What is selection sort?

What is bubble sort?

What is binary search?

CS 105 Spring 2010

Page 2: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 2CS 105 Spring 2010

Excel Sorting Data

Excel Sorting Data 1. select

2. choose the type of sort in the Data tab

Page 3: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 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.

Page 4: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 4CS 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.

Page 5: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 5CS 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

Page 6: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 6CS 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 intPositionEnd Sub

Page 7: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 7CS 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.

Page 8: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 8CS 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

Page 9: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 9CS 105 Spring 2010

Sub BubbleSort(strNames() As String, intCount As Integer)

Dim intIndex As Integer, intPosition As IntegerDim 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

Page 10: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 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 dependenton the particular hardware on which the test is performed.

Counting the number of comparisons and the number of swaps is independentof the hardware.

Page 11: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 11CS 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.

Page 12: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 12CS 105 Spring 2010

Sub BubbleSort(strNames() As String, intCount As Integer)

Dim intIndex As Integer, intPosition As IntegerDim strTemp As StringDim 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 SubEnd If

Next intPosition

End Sub

Page 13: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 13

Searching Algorithms

• Binary Search

Sorting makes searching easier. There are numerous ways to perform a search. We will consider just one:

Page 14: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 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

Page 15: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 15CS 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

Page 16: # 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.

# 16CS 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