Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Plus Data Structures ADTs Unsorted List and Sorted List
Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 4 ADT Sorted List.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
C++ Plus Data Structures
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 1400 March 30, 2007 Chapter 8 Searching and Sorting.
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 Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 8 ARRAYS Continued
Chapter 4 ADT Sorted List.
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.
Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
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)
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
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 & 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.
Programming at a high level. Developing a Computer Program Programmer  Writes program in source code (VB or other language) Compiler  Converts source.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Chapter 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
CSI 1340 Introduction to Computer Science II Chapter 3 ADTs Unsorted List and Sorted List.
1 Chapter 4 ADT Sorted List. 2 Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem.
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.
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.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Searching and Sorting Arrays
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Introduction to Search Algorithms
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
C++ Plus Data Structures
Searching and Sorting Arrays
Searching and Sorting Arrays
Presentation transcript:

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays

Chapter 9 slide 2 Topics 9.1 Introduction to Search Algorithms

Chapter 9 slide Introduction to Search Algorithms Search: locate an item in a list (array, vector, etc.) of information Two algorithms (methods): –Linear search –Binary search

Chapter 9 slide 4 Linear Search Algorithm Set found to false Set position to –1 Set index to 0 While index < number of elts and found is false If list [index] is equal to search value found = true position = index End If Add 1 to index End While Return position

Chapter 9 slide 5 Linear Search Example Array numlist contains Searching for the the value 11, linear search examines 17, 23, 5, and 11 Searching for the the value 7, linear search examines 17, 23, 5, 11, 2, 29, and

Chapter 9 slide 6 Linear Search Tradeoffs Benefits –Easy algorithm to understand –Array can be in any order Disadvantage –Inefficient (slow): for array of N elements, examines N/2 elements on average for value in array, N elements for value not in array

Chapter 9 slide 7 Binary Search Algorithm 1.Divide a sorted array into three sections. –middle element –elements on one side of the middle element –elements on the other side of the middle element 2.If the middle element is the correct value, done. Otherwise, go to step 1, using only the half of the array that may contain the correct value. 3.Continue steps 1 and 2 until either the value is found or there are no more elements to examine.

Chapter 9 slide 8 Binary Search Example Array numlist2 contains Searching for the the value 11, binary search examines 11 and stops Searching for the the value 7, binary search examines 11, 3, 5, and stops

Trace of Binary Search info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item = 45 first midPoint last info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first midPoint last LESS last = midPoint - 1 GREATERfirst = midPoint + 1

Trace continued info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item = 45 first, midPoint, last info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, last midPoint LESS last = midPoint - 1GREATERfirst = midPoint + 1

Trace concludes info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] item = 45 last first first > last found = false

bool BinarySearch ( ArrayType info, ItemType& item, int length) // Purpose: To determine whether item is in the array info // Returns: If found, item’s key matches an element’s key in the list and a copy // of that element has been stored in item and returns true; otherwise, item is // unchanged and returns false { bool fount = false; int midPoint ; int first = 0; intlast = length - 1 ; bool moreToSearch = ( first <= last ) ; while ( moreToSearch && !found ) {midPoint = ( first + last ) / 2 ;// INDEX OF MIDDLE ELEMENT switch ( item.ComparedTo( info [ midPoint ] ) ) { case LESS : last = middle -1; break; // LOOK IN FIRST HALF NEXT case GREATER : first = middle+1; break; // LOOK IN SECOND HALF NEXT case EQUAL : found = true; // ITEM HAS BEEN FOUND item.Copy ( info[midPoint] ); } return found; }

Chapter 9 slide 13 Binary Search Tradeoffs Benefit –Much more efficient than linear search (For array of N elements, performs at most log 2 N comparisons) Disadvantage –Requires that array elements be sorted