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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Binary Search Visualization i j.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
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)
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
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.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will –Learn about arrays One-dimensional arrays Two-dimensional arrays –Learn about searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
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.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
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.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
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.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
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.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Programming games in Visual Basic Review programming & VB topics Insertion sort. Best times. Generate questions & answer patterns for quiz Lab/Homework:
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Searching Topics Sequential Search Binary Search.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Section 1.7 Comparing Algorithms: Big-O Analysis.
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
Chapter 7 Arrays.
Searching and Sorting Arrays
Searching CLRS, Sections 9.1 – 9.3.
Searching.
Presentation transcript:

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

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

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

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

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

Visual C++ Programming 6

7

8

9

10

Visual C++ Programming 11

Visual C++ Programming 12

Visual C++ Programming 13

Visual C++ Programming 14

Visual C++ Programming 15

Visual C++ Programming 16

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

Visual C++ Programming 18

Visual C++ Programming 19

Visual C++ Programming 20

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?

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

Visual C++ Programming 23

Visual C++ Programming 24

Visual C++ Programming 25

Visual C++ Programming 26

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

Visual C++ Programming 28

Visual C++ Programming 29

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

Visual C++ Programming 31

Visual C++ Programming 32

Visual C++ Programming 33

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)

Visual C++ Programming 35

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);

Visual C++ Programming 37

Visual C++ Programming 38

Visual C++ Programming 39

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);

Visual C++ Programming 41

Visual C++ Programming 42

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)

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 >, <

Visual C++ Programming 45

Visual C++ Programming 46

Visual C++ Programming 47

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

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)