CS 1044 Intro Programming in C++ Fall 2002

Slides:



Advertisements
Similar presentations
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CSC 211 Data Structures Lecture 13
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Alg Analysis Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2006 ©2006 McQuain & Ribbens Algorithms algorithm: a finite set of.
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Analysis of Algorithms
Searching Given a collection and an element (key) to find… Output
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Announcements HW6 due this Wednesday
Search Recall the employee database used in class examples: employeeRecordT workforce[5] = Given "Vance, Emelline", how can we find her salary? Name.
Algorithm Analysis CSE 2011 Winter September 2018.
Announcements Final Exam on August 17th Wednesday at 16:00.
Design by Contract Fall 2016 Version.
Arrays … The Sequel Applications and Extensions
Searching CSCE 121 J. Michael Moore.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CS 3343: Analysis of Algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Algorithms Chapter 3 With Question/Answer Animations
Algorithm design and Analysis
Searching.
CSC212 Data Structure - Section RS
Chapter 8 Search and Sort
Chapter 5 Arrays Introducing Arrays
Array of Structures Contrast this with the corresponding parallel array example in the array chapter. Using an array of structures instead offers several.
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Data Structures Review Session
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Announcements HW6 due this Wednesday
Searching and Sorting 1-D Arrays
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Ken Lambert & Doug Nance
Searching CLRS, Sections 9.1 – 9.3.
Topic 24 sorting and searching arrays
COMPUTER 2430 Object Oriented Programming and Data Structures I
7 Arrays.
Searching and Sorting Arrays
CSC212 Data Structure - Section KL
Chapter 23 Searching and Sorting
The Binary Search by Mr. Dave Clausen
CS 2604 Data Structures and File Management
Presentation transcript:

CS 1044 Intro Programming in C++ Fall 2002 April 16, 2019 Searching In many applications it is necessary to search a list of data elements for one (or more) that matches some specific criterion. For example: - find the largest integer in a list of test scores - find the location of the string "Fred" in a list of names - find all Trip variables whose destination is "Jackson, WY" There are several basic issues when searching: - how do we recognize and keep track of matches to the search criteria? - how do we terminate the search? - what do we report? (value, location, yes/no, . . .) - how do we recognize that there is no matching data element? - what do we do if no matching data element is found? Here we will only consider the problem of searching among the elements of an array. Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD Searching

CS 1044 Intro Programming in C++ Fall 2002 April 16, 2019 Simple Array Search Returning a dummy value to indicate failure is only a sensible option if there are reasonable values of the correct type that could not, logically, be data values. If you're storing test scores or areas of polygons or names of presidents then there are good candidates. If you're storing characters entered by a user at the keyboard or values of a polynomial function, good candidates are hard to find. Consider searching the given array for the values 5 and 41. Trace the logic, paying attention to the stop criteria and what information might be returned in each case. There's also the issue of what to do in the event that there are multiple "hits" on the given search criteria. Returning the first match and stopping is a common approach, but it's not always suitable. Point is: this is an issue that must be considered, handled, and documented. When searching an array of simple data elements, we may resolve these questions easily: - We recognize matches by comparing with the equality operator. - We terminate the search if we find an element that equals the search target, or if we have rejected the last used cell in the array. - We may report a match by providing the index or a copy of the element. - We recognize that there is no matching data element if we reach the end of the used cells in the array without finding a match. - If no matching element is found, we return an impossible index (e.g., -1) or a dummy value that cannot logically be a valid data element. unused A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11] A[12] A[13] A 13 7 43 5 3 19 2 23 29 ?? Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD Searching

Linear Search Function CS 1044 Intro Programming in C++ Fall 2002 April 16, 2019 Linear Search Function Questions: should Size be the dimension or the usage of List[]? why doesn't the loop condition cause an access violation if the dimension of List[] is Size and there is no match? where does the declaration of MISSING go? what will this function return if there are multiple occurrences of Target in List[]? An alternative implementation (my preference actually) would be: for (Scan = 0; Scan < Size; Scan++) { if ( Target == List[Scan] ) return Scan; } return MISSING; The simplest technique for searching a list is to just "walk" the list, examining each element in turn until you either find a match or reject the last used cell: const int MISSING = -1 ; int LinearSearch(const int List[], int Target, int Size) { int Scan = 0; // begin search at first cell // Continue searching while there are more entries // to consider and the target value has not been // found: while ( ( Scan < Size ) && (List[Scan] != Target) ) Scan++ ; if ( Scan < Size ) // Target was found return( Scan ); else return ( MISSING ); // Target was not found } Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD Searching

Application: a Simple Database CS 1044 Intro Programming in C++ Fall 2002 April 16, 2019 Application: a Simple Database This is a fairly standard example of the terminal CS 1044 project. There should be a complete formal specification on the course website by the time these notes are reached, and a formal design analysis, as well as the complete source code for a working implementation. The following slides will present portions of that implementation, along with some design discussion. Consider implementing an application that will store data for a list of trips and perform lookup operations on that list (e.g., report the distance or time for a trip given a specific origin and destination). Clearly we may use an array of Trip variables, as defined earlier, to store the data. Assume that the application will use two input files, one containing the trip data as we've seen before, and a second input file that will contain the lookup commands the program is supposed to process, using the general syntax: <command string><tab><tab-separated command parameters> For example: mileage Knoxville, TN Nashville, TN time Tucumcari, NM Albuquerque, NM neighbors Birmingham, AL . . . Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD Searching

Searching a List of Structured Data CS 1044 Intro Programming in C++ Fall 2002 April 16, 2019 Searching a List of Structured Data The search function is deliberately designed (by ignoring trip direction) to be a bit less than trivial. An alternative would be to pass the origin and destination strings "naked". Wrapping them inside a Trip variable allows us to make the caller unaware of the internal structure of a Trip (and simplifies the parameter list, although not the implementation of the test). Is there be anything wrong with the alternative test code below? if ( (toFind.Origin == dB[Idx].Origin || toFind.Origin == dB[Idx].Destination) && (toFind.Destination == dB[Idx].Destination || toFind.Destination == dB[Idx].Origin) ) { . . . In this case, the array elements are complex structures and the search function must access the appropriate member(s) to make the comparison: int reportMileage(const Trip& toFind, const Trip dB[], int numTrips) { int Idx; for (Idx = 0; Idx < numTrips; Idx++) { if ( (toFind.Origin == dB[Idx].Origin && toFind.Destination == dB[Idx].Destination) || (toFind.Origin == dB[Idx].Destination && toFind.Destination == dB[Idx].Origin) ) { return dB[Idx].Miles; } return MISSING; The designer has determined that the direction of the trip does not matter, which complicates the Boolean test for a match somewhat. How would the implementation change if direction did matter? Note the use of const in the parameter list. Is this good design? Why or why not? Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD Searching

CS 1044 Intro Programming in C++ Fall 2002 April 16, 2019 Binary Search Binary search is often called bisection search. Overemphasize the fact that binary search is only guaranteed to work correctly on a sorted list. It doesn't hurt to trace the execution (later) on an unsorted list to demonstrate a false negative. Linear search will always work, but there is an alternative that is, on average, much faster. The difficulty is that binary search may only be applied under the following: Assumption: the list is sorted in ascending (or descending) order: List[0] <= List[1] <= ... <= List[Size-1] Binary search: Examine the middle element. If the target element is not found, determine to which side it falls. Divide that section in half and examine the middle element, etc, etc ... ? x y z Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD Searching

Binary Search Function CS 1044 Intro Programming in C++ Fall 2002 April 16, 2019 Binary Search Function It's very important to discuss (again) the need to be sure that the index values passed in are correct. Both wrt searching and sorting, remind the students continually of the importance of remembering the distinction between the dimension of the array and the usage (the number of cells holding real data). Discuss what would happen if a search or sort algorithm were applied using the dimension as the upper limit. This is a classic error, and students make it a LOT, even after being warned. You CANNOT emphasize the distinction too much. An implementation of binary search is more complex logically than a linear search, but the performance gain is impressive: const int MISSING = -1; int BinSearch(const int List[] , int Target, int Lo, int Hi) { int Mid; while ( Lo <= Hi ) { Mid = ( Lo + Hi ) / 2; // find "middle" index if ( List[Mid] == Target ) // check for target return ( Mid ); else if ( Target < List[Mid] ) Hi = Mid - 1; // look in lower half else Lo = Mid + 1; // look in upper half } return ( MISSING ); // Target not found } Note this implementation allows the caller to search a specified portion of List[]. Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD Searching

CS 1044 Intro Programming in C++ Fall 2002 April 16, 2019 Binary Search Trace Be careful with the termination condition on the second (unsuccessful) search. Consider performing a binary search for the value 2 using the array given on slide 2. Suppose an array contains the values: 1 2 3 4 5 6 7 8 9 10 11 12 13 A 21 35 41 43 51 82 85 86 93 ?? Consider searching A[] for the value 86. What would the call look like? Trace the execution: Lo Mid Hi Consider searching A[] for the value 50. What would the call look like? Trace the execution: Lo Mid Hi Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD Searching

CS 1044 Intro Programming in C++ Fall 2002 April 16, 2019 Cost of Searching For CS 1044 this is purely optional material… it does serve to illustrate the difference a good algorithm can make (ignoring the cost of building a sorted list for searching). Suppose the array to be searched contains N elements. The cost of searching may be measured by the number of array elements that must be compared to Target. Using that measure: Best Case Worst Case Average Case Linear Search 1 N N/2 Binary Search 1 log2N log2N To get an idea of how much cheaper binary search is, note that N = 1024 log2N = 10 N = 10242 log2N = 20 Of course, binary search is only feasible if the array is sorted . . . Computer Science Dept Va Tech August, 2002 ©1995-2002 Barnette ND & McQuain WD Searching