Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)

Similar presentations


Presentation on theme: "Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)"— Presentation transcript:

1 Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)

2 Objectives In this chapter, you will: Learn about the binary search algorithm Compare the binary search to other common forms of searching Analyze the complexity (big-O) of the binary search Create and use arrays of strings Programming with Visual C++2

3 Objectives (continued) Compare strings Explore uses for the ListView control Programming with Visual C++3

4 Searching a Sorted List Sorted lists (like telephone directories) can be searched in a more efficient way than by sequential search Strategy – Repeatedly divide the search domain in half and half again until the name is found This is called binary search because the search domain is divided into two parts Programming with Visual C++4

5 Binary Search Algorithm Determine the beginning of the search domain (low) Determine the end of the search domain (high) Determine the middle (mid) Compare the target value to the value in mid – If the target < the value in mid, then reset high – Else if the target > the value in mid, then reset low – Repeat the process until the target matches the value in mid or the list cannot be divided any further Programming with Visual C++5

6 Binary Search Algorithm (continued) Programming with Visual C++6

7 Binary Search Algorithm (continued) Programming with Visual C++7

8 Binary Search Algorithm (continued) Programming with Visual C++8

9 Binary Search Algorithm (continued) Programming with Visual C++9

10 Binary Search Algorithm (continued) Programming with Visual C++10

11 Binary Search Algorithm (continued) Programming with Visual C++11

12 Binary Search Algorithm (continued) Programming with Visual C++12

13 Binary Search Algorithm (continued) Programming with Visual C++13

14 Binary Search Algorithm (continued) Programming with Visual C++14

15 Binary Search Algorithm (continued) Programming with Visual C++15

16 Binary Search Example Binary search program example – Display array in txtList – When btnSearch is clicked: Read target value from txtTarget Call binary search method Binsearch() Display “FOUND!” or “NOT FOUND!” in MessageBox Programming with Visual C++16

17 Binary Search Example (continued) Programming with Visual C++17

18 Programming with Visual C++18

19 Programming with Visual C++19

20 Search Analysis Three methods to find a target value in an array – Direct lookup – Sequential search – Binary search What situations determine when each is used? How do we compare them? Programming with Visual C++20

21 Direct Lookup Direct lookup uses only one operation to find the target The target value must match an array index value For example: If the index values on an array run from 0-7, then the target value must be an integer 0-7 Lookup strategy Read the target and use it as an array element subscript to get the value you want Example: volleyball team player data Players have numbers 0 through 7 Use the number to look up data for that player Programming with Visual C++21

22 Direct Lookup (continued) Programming with Visual C++22

23 Direct Lookup (continued) Programming with Visual C++23 Direct lookup example – It takes one operation to look up the number of assists a volleyball player has

24 Direct Lookup (continued) Programming with Visual C++24

25 Direct Lookup (continued) No matter how many elements are in an array, direct lookup will always find what it is looking for with a single operation This makes direct lookup the fastest search method available Unfortunately, the limitation is that the target must match an array element index – You cannot search by anything else – This is very restricting Programming with Visual C++25

26 Direct Lookup (continued) Programming with Visual C++26

27 Sequential Search In a sequential search, the target value is the same type as the array to be searched Data values in the array need not be in order Search outcome scenarios for an array of n elements – Worst case: n comparisons – Average case: n/2 comparisons – Best case: 1 comparison Programming with Visual C++27

28 Sequential Search (continued) Programming with Visual C++28

29 Sequential Search (continued) The more items there are in an array, the longer, on average, a sequential search will take The worst-case scenario – If there are n items, it will take n comparisons to find the target or determine that it is not in the list – The larger the array, the more comparisons are needed and the slower the search will be Programming with Visual C++29

30 Sequential Search (continued) Programming with Visual C++30

31 Binary Search The target value is of the same type as the array to be searched Data values in the array must be in sorted order Search outcome scenarios for an array of n elements – Worst case: log 2 n comparisons – Best case: 1 comparison Programming with Visual C++31

32 Binary Search (continued) Programming with Visual C++32

33 Binary Search (continued) There is a limit to how many times a binary search can divide a list in half before it arrives at the final element Programming with Visual C++33

34 Binary Search (continued) Programming with Visual C++34 The worst-case scenario is log 2 n This means that the number of operations increases as n increases, but not very much The only problem is that the binary search requires a sorted array

35 Binary Search (continued) Programming with Visual C++35

36 Determining the Best Approach The complexity of an algorithm is measured by a complexity function called big-O Big-O is based on worst-case scenarios Direct lookup is big-O(1) Sequential search is big-O(n) Binary search is big-O(log 2 n) Programming with Visual C++36

37 Determining the Best Approach (continued) Programming with Visual C++37

38 Searching for Strings A String is a system-defined class An array is a system-defined class that can contain objects of any type – A handle ( ^ ) is a pointer to a system-defined object – In this case, nameArr is a handle for an array object that will contain handles for String objects Programming with Visual C++38

39 Searching for Strings (continued) To construct an array, use gcnew to make an array of 20 String handles This process initializes nameArr, but does not put anything in the 20 String elements Programming with Visual C++39

40 Searching for Strings (continued) Programming with Visual C++40

41 Searching for Strings (continued) An array object has built-in methods SetValue() is used to assign a value to an array element SetValue() has two parameters – The data value (in this case a String ) – The index value of the element it is assigned to – nameArr->SetValue(“Star Wars”,4); Programming with Visual C++41

42 Searching for Strings (continued) Programming with Visual C++42

43 Searching for Strings (continued) Programming with Visual C++43

44 Searching for Strings (continued) For the binary search, data must be sorted Array class methods can sort array data – Array::Sort(nameArr); – By default, data is sorted in ascending order (low to high) Programming with Visual C++44

45 Searching for Strings (continued) Relational operators (like >, ==, <=) are not defined for String objects The String class, however, defines methods that are able to compare two String s String class methods – Comparing two String objects – String::Compare(String1, String2) – Example: String::Compare(“LOST”,”FOUND”); Programming with Visual C++45

46 Searching for Strings (continued) Result of String::Compare(String1, String2) – Returns 1 if String1 > String2 – Returns 0 if String1 is identical to String2 – Returns -1 if String1 < String2 The ASCII codes of corresponding characters are used to determine whether one is greater, less than, or equal to the other Programming with Visual C++46

47 Searching for Strings (continued) Programming with Visual C++47 If String::Compare() returns 0, then it means the two String objects are identical

48 Searching for Strings (continued) Programming with Visual C++48 The ToUpper() method provides the uppercase version of a String

49 Searching for Strings (continued) Programming with Visual C++49 For the purposes of finding a target, you should compare String s of the same case Otherwise “Jaws”, “JAWS”, and “jaws” will not be considered the same name

50 Summary Binary search algorithms are used for common tasks, like looking up a name in a phone book The binary search divides the list in half and half again Binary search uses three key positions Lowest location in searchable domain (low) Highest location in searchable domain (high) Middle location A while loop performs the repeated division of the list Programming with Visual C++50

51 Summary (continued) Search analysis Direct search Requires target value to be an array index value Complexity measure is big-O(1) Sequential search Data may be unsorted Target value is same data type as the array Complexity measure is big-O(n) Binary search Data must be sorted Complexity measure is big-O(log 2 n) Programming with Visual C++51

52 Summary (continued) The array class is used to create arrays of objects The array class has a built-in Sort() method To compare one String to another, use the String method Compare() Compare returns 1, 0, or 1 depending on whether the first String was >, =, or < than the second Programming with Visual C++52


Download ppt "Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)"

Similar presentations


Ads by Google