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

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Binary Search Visualization i j.
Searching and Sorting I 1 Searching and Sorting 1.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Chapter 8 ARRAYS Continued
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
1 Searching and Sorting Linear Search Binary Search.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
CSC 211 Data Structures Lecture 13
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Searching Topics Sequential Search Binary Search.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 8A Binary Search (Concepts)
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Copyright Prentice Hall Modified by Sana odeh, NYU
Chapter 9: Sorting and Searching Arrays
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Searching and Sorting Arrays
Searching CLRS, Sections 9.1 – 9.3.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Programming with Visual C++18

Programming with Visual C++19

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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