Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 2.6: Searching and Sorting

Similar presentations


Presentation on theme: "Section 2.6: Searching and Sorting"— Presentation transcript:

1 Section 2.6: Searching and Sorting
Binary Search

2 Introduction In this presentation you will see how the binary search algorithm works. To do this a list containing 10 names will be searched using the algorithm. The pseudo code will be traced through step-by-step and the results of each stage shown. Three examples of the algorithm being used are given.

3 Integer Division The binary search algorithm makes use of the DIV function DIV returns the whole number part of integer division 6 DIV 2 = 3 7 DIV 2 = 3 5 DIV 2 = 2

4 Algorithm On the left is an algorithm for a binary search.
Found is a Boolean variable that is used to indicate if the item has been found in the list. ItemSought is used to store the item we are looking for. SearchFailed is a Boolean variable that is used if it has been discovered that the item is not in the list. Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed

5 Search 1 In this first example, the binary search algorithm is going to be used to find the name “Thor” in this list: 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val

6 Searching for Thor Thor False False 1 10 5 6
Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed In this example we are going to use a binary search. Our list is called “List” and has 10 elements in it. We are going to search for “Thor”. First is a variable that is used to indicate where the beginning of the sublist being searched is. This changes as the algorithm is executed. It is 1 to start with. Last is a variable that is used to indicate where the end of the sublist being searched is. This changes as the algorithm is executed. It is 10 to start with. SearchFailed is a Boolean variable that is used to indicate if we have found out the item is not in the list. It is set to False initially. Found is a Boolean variable that is used to indicate if the item has been found in the list. It is set to False initially. ItemSought is the variable that is used to store the item we are searching for. 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val ItemSought Mid Found SearchFailed First Last Thor False False 1 10 5 6 Found = false SearchFailed = false So the code gets repeated again... Mid = 5 First is assigned the value of Mid + 1 (i.e. 6) Mid = 5 List[Mid] = “Fred” Does List[Mid] = “Thor”? NO First = 1 Last = 10 First + Last = 11 11 DIV 2 = 5 Mid is assigned the value of 5 So the else part is next... So the else part is next... First = 1 Last = 10 First is not greater than or equal to last So the else part is next... Mid = 5 List[Mid]=“Fred” “Fred” is not greater than “Thor”

7 Searching for Thor Thor False False 1 10 5 6 8 9 Mid = 8
Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val ItemSought Mid Found SearchFailed First Last Thor False False 1 10 5 6 8 9 Mid = 8 First is assigned the value of Mid + 1 (i.e. 9) Found = false SearchFailed = false So the code gets repeated again... Mid = 8 List[Mid] = “Matt” Does List[Mid] = “Thor”? NO First = 6 Last = 10 First + Last = 16 11 DIV 2 = 8 Mid is assigned the value of 8 So the else part is next... First = 6 Last = 10 First is not greater than or equal to last So the else part is next... So the else part is next... Mid = 8 List[Mid]=“Matt” “Matt” is not greater than “Thor”

8 Searching for Thor Thor False False 1 10 5 6 8 9 9 True Found = true
Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val ItemSought Mid Found SearchFailed First Last Thor False False 1 10 5 6 8 9 9 True Found = true SearchFailed = false So the code does not get repeated again as Thor has been found in the list. Mid = 9 List[Mid] = “Thor” Does List[Mid] = “Thor”? YES First = 9 Last = 10 First + Last = 19 11 DIV 2 = 9 Mid is assigned the value of 9 So the then part is next... ...and Found is assigned the value of True

9 Search 2 In the second example, the binary search algorithm is going to be used to find the name “Dan” in the same list. 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val

10 Searching for Dan Dan False False 1 10 5 4 Mid = 5
Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val ItemSought Mid Found SearchFailed First Last Dan False False 1 10 5 4 Mid = 5 Last is assigned the value of Mid - 1 (i.e. 4) Mid = 5 List[Mid] = “Fred” Does List[Mid] = “Dan”? NO Found = false SearchFailed = false So the code gets repeated again... First = 1 Last = 10 First + Last = 11 11 DIV 2 = 5 Mid is assigned the value of 5 So the then part is next... So the else part is next... So the else part is next... First = 1 Last = 10 First is not greater than or equal to last Mid = 5 List[Mid]=“Fred” “Fred” is greater than “Dan”

11 Searching for Dan Dan False False 1 10 5 4 2 3 Mid = 2
Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val ItemSought Mid Found SearchFailed First Last Dan False False 1 10 5 4 2 3 Mid = 2 First is assigned the value of Mid + 1 (i.e. 3) Found = false SearchFailed = false So the code gets repeated again... Mid = 2 List[Mid] = “Bob” Does List[Mid] = “Dan”? NO First = 1 Last = 4 First + Last = 5 5 DIV 2 = 2 Mid is assigned the value of 2 So the else part is next... First = 1 Last = 4 First is not greater than or equal to last So the else part is next... So the else part is next... Mid = 2 List[Mid]=“Bob” “Bob” is not greater than “Dan”

12 Searching for Dan Dan False False 1 10 5 4 2 3 3 True Found = true
Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val ItemSought Mid Found SearchFailed First Last Dan False False 1 10 5 4 2 3 3 True Found = true SearchFailed = false So the code does not get repeated again – as Dan has been found in the list Mid = 3 List[Mid] = “Dan” Does List[Mid] = “Dan”? YES First = 3 Last = 4 First + Last = 7 7 DIV 2 = 3 Mid is assigned the value of 3 So the then part is next... ...and Found is assigned the value of True

13 Search 3 In the third example, the binary search algorithm is going to be used to find the name “Ann” in the same list. 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val

14 Searching for Ann Ann False False 1 10 5 4 Mid = 5
Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val ItemSought Mid Found SearchFailed First Last Ann False False 1 10 5 4 Mid = 5 Last is assigned the value of Mid - 1 (i.e. 4) Mid = 5 List[Mid] = “Fred” Does List[Mid] = “Ann”? NO Found = false SearchFailed = false So the code gets repeated again... First = 1 Last = 10 First + Last = 11 11 DIV 2 = 5 Mid is assigned the value of 5 So the then part is next... So the else part is next... So the else part is next... First = 1 Last = 10 First is not greater than or equal to last Mid = 5 List[Mid]=“Fred” “Fred” is greater than “Ann”

15 Searching for Ann Ann False False 1 10 5 4 2 1
Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val ItemSought Mid Found SearchFailed First Last Ann False False 1 10 5 4 2 1 Mid = 2 List[Mid] = “Bob” Does List[Mid] = “Ann”? NO First = 1 Last = 4 First + Last = 5 5 DIV 2 = 2 Mid is assigned the value of 2 So the else part is next... First = 1 Last = 4 First is not greater than or equal to last So the else part is next... So the then part is next... Found = false SearchFailed = false So the code gets repeated again... Mid = 2 Last is assigned the value of Mid - 1 (i.e. 1) Mid = 2 List[Mid]=“Bob” “Bob” is greater than “Ann”

16 Searching for Ann Ann False False 1 10 5 4 2 1 True 1 First = 1
Repeat Mid  (First + Last) DIV 2 If List[Mid] = ItemSought Then Found  True Else If First >= Last Then SearchFailed  True If (List[Mid] > ItemSought) Then Last  Mid – 1 First  Mid + 1 EndIf Until Found OR SearchFailed 1 2 3 4 5 6 7 8 9 10 Ben Bob Dan Eve Fred Kate Lily Matt Thor Val ItemSought Mid Found SearchFailed First Last Ann False False 1 10 5 4 2 1 True 1 First = 1 Last = 1 First + Last = 2 2 DIV 2 = 1 Mid is assigned the value of 1 Found = false SearchFailed = true So the code does not repeat again, and it is now known that Ann is not in the list. So the then part is next... ... and searchFailed is assigned a value of true. Mid = 1 List[Mid] = “Ben” Does List[Mid] = “Ann”? NO First = 1 Last = 1 First is greater than or equal to last (it is equal to) So the else part is next...

17 Search 3 - Conclusion In the third example, the name “Ann” is not in the list, so the search failed.


Download ppt "Section 2.6: Searching and Sorting"

Similar presentations


Ads by Google