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 Visual C++ Programming 2  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

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

4 Searching a Sorted List Visual C++ Programming 4  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

5 Binary Search Algorithm Visual C++ Programming 5  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

6 Visual C++ Programming 6

7 7

8 8

9 9

10 10

11 Visual C++ Programming 11

12 Visual C++ Programming 12

13 Visual C++ Programming 13

14 Visual C++ Programming 14

15 Visual C++ Programming 15

16 Visual C++ Programming 16

17 Binary Search Example Visual C++ Programming 17  Display array in txtList  When btnSearch is clicked  Read target value from TextBox  Use binary search  Display “FOUND!” or “NOT FOUND!” in MessageBox

18 Visual C++ Programming 18

19 Visual C++ Programming 19

20 Visual C++ Programming 20

21 Search Analysis Visual C++ Programming 21  Three methods to find a target value in an array  Direct lookup  Sequential search  Binary search  What situations determine when each is used?  How can their complexity be assessed?

22 Direct Lookup Visual C++ Programming 22  The target values match array index values  For example: if the index values on an array run from 0-7 then the target value must also be an integer in the range 0-7  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-7 Use the number to look up data for that player  Only one operation required

23 Visual C++ Programming 23

24 Visual C++ Programming 24

25 Visual C++ Programming 25

26 Visual C++ Programming 26

27 Sequential Search Visual C++ Programming 27  The target value is of 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

28 Visual C++ Programming 28

29 Visual C++ Programming 29

30 Binary Search Visual C++ Programming 30  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

31 Visual C++ Programming 31

32 Visual C++ Programming 32

33 Visual C++ Programming 33

34 Determining the Best Approach Visual C++ Programming 34  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)

35 Visual C++ Programming 35

36 Searching for Strings Visual C++ Programming 36  Strings are a system-defined class (not a primitive type)  An array of Strings  Use system-defined array class  Can contain any data type (not just primitive types) private: array ^ nameArr;  Must be constructed nameArr = gcnew array (20);

37 Visual C++ Programming 37

38 Visual C++ Programming 38

39 Visual C++ Programming 39

40 Searching for Strings (continued) Visual C++ Programming 40  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  The index value of the element it is assigned to  nameArr->SetValue(“Star Wars”,4);

41 Visual C++ Programming 41

42 Visual C++ Programming 42

43 Searching for Strings (continued) Visual C++ Programming 43  Array class methods  For the binary search data must be sorted  The array class provides this capability  Array::Sort(nameArr);  By default data is sorted in ascending order (low to high)

44 Searching for Strings (continued) Visual C++ Programming 44  String class methods  Syntax: String::Compare(String1, String2)  Compares two String arguments  Example: String(Compare(“LOST”,”FOUND”);  Result of String::Compare()  Return 1 If String1 > String2  Return 0 if String1 is identical to String2  Return -1 if String1 < String2  ASCII codes determine >, <

45 Visual C++ Programming 45

46 Visual C++ Programming 46

47 Visual C++ Programming 47

48 Summary Visual C++ Programming 48 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

49 Summary (continued) Visual C++ Programming 49 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)


Download ppt "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 8A Binary Search (Concepts)"

Similar presentations


Ads by Google