Search algorithms for vectors

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Recursion.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Searching. 2 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. 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
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Matrices Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
Sequences Jordi Cortadella Department of Computer Science.
Searching CS 105 See Section 14.6 of Horstmann text.
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. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Complexity Analysis of Algorithms
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Vectors Jordi Cortadella Department of Computer Science.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Searching CS 110: Data Structures and Algorithms First Semester,
Functions Jordi Cortadella Department of Computer Science.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Searching Arrays Linear search Binary search small arrays
16 Searching and Sorting.
19 Searching and Sorting.
Searching.
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Reasoning with invariants
Searching Given a collection and an element (key) to find… Output
Jordi Cortadella Department of Computer Science
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Jordi Cortadella and Jordi Petit Department of Computer Science
Announcements Final Exam on August 17th Wednesday at 16:00.
Topic 14 Searching and Simple Sorts
Jordi Cortadella Department of Computer Science
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.
Searching.
CSC212 Data Structure - Section RS
Complexity Analysis of Algorithms
Chapter 9 One-Dimensional Arrays
MSIS 655 Advanced Business Applications Programming
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Ken Lambert & Doug Nance
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
Topic 14 Searching and Simple Sorts
Containers: Queue and List
Jordi Cortadella and Jordi Petit Department of Computer Science
Jordi Cortadella Department of Computer Science
Searching.
Binary Search and Loop invariants
CSC212 Data Structure - Section KL
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Searching and Sorting Hint at Asymptotic Complexity
The Binary Search by Mr. Dave Clausen
Searching and Sorting Hint at Asymptotic Complexity
Searching.
Presentation transcript:

Search algorithms for vectors Jordi Cortadella Department of Computer Science

Search in a vector We want to design a function that searches for a value in a vector. The function must return the index of the location in which the value is found. It must return -1 if not found. If several locations contain the search value, it must return the index of one of them. Specification: // Pre: A is vector. // Returns i, such that A[i] == x, if x is in A. // Returns -1 if x is not in A. Introduction to Programming © Dept. CS, UPC

Search in a vector Invariant: x does not exist in A[0..i-1]. 3 -4 1 2 Note: an interval A[p..q] with p > q is assumed to be an empty interval. 3 -4 1 2 15 9 6 4 i Introduction to Programming © Dept. CS, UPC

Search in a vector // Returns i, such that A[i] == x, if x is in A. // Returns -1 if x is not in A. int search(int x, const vector<int>& A) { // Inv: x does not exist in A[0..i-1]. for (int i = 0; i < A.size(); ++i) { if (A[i] == x) return i; } return -1; } Introduction to Programming © Dept. CS, UPC

Search with sentinel The previous code has a loop with two conditions: i < A.size(): to detect the end of the vector A[i] == x: to detect when the value is found The search is more efficient if the first condition is avoided (if we ensure that the value is always in the vector). To enforce this condition, a sentinel may be added in the last (unused) location of the vector. When the sentinel is found, it indicates that the value was not anywhere else in the vector. Introduction to Programming © Dept. CS, UPC

Be careful: not a const parameter Search with sentinel // Returns i, such that A[i] == x, if x is in A. // Returns -1 if x is not in A. // Post: the vector is temporarily modified, but the // final contents remains unchanged. int search(int x, vector<int>& A) { int n = A.size(); A.push_back(x); // Writes the sentinel int i = 0; // Inv: x does not exist in A[0..i-1] while (A[i] != x) ++i; A.pop_back(); // Removes the sentinel if (i == n) return -1; return i; } Be careful: not a const parameter Introduction to Programming © Dept. CS, UPC

How would you search in a dictionary? Dictionaries contain a list of sorted words. To find a word in a dictionary of 50,000 words, you would never check the first word, then the second, then the third, etc. Instead, you would look somewhere in the middle and decide if you have to continue forwards or backwards, then you would look again around the middle of the selected part, go forwards/backwards, and so on and so forth … Introduction to Programming © Dept. CS, UPC

Binary search Is 4 in the vector? 4 is larger 4 is smaller Found ! -9 -7 -6 -4 -1 1 3 4 5 7 8 9 4 is larger -9 -7 -6 -4 -1 1 3 4 5 7 8 9 Half of the elements have been discarded ! 4 is smaller -9 -7 -6 -4 -1 1 3 4 5 7 8 9 Found ! Introduction to Programming © Dept. CS, UPC

Binary search How many iterations do we need in the worst case? The search will finish when only one element is left: iteration 1 2 3 4 5 6 7 i elements n n/2 n/4 n/8 n/16 n/32 n/64 n/128 n/2i Introduction to Programming © Dept. CS, UPC

Binary search Invariant: If x is in vector A, then it will be found in fragment A[left...right] -9 -7 -6 -4 -1 1 3 4 5 7 8 9 left right The search will be completed when the value has been found or the interval is empty (left > right) Introduction to Programming © Dept. CS, UPC

Binary search // Pre: A is sorted in ascending order, // 0 <= left,right < A.size() // Returns the position of x in A[left...right] // (returns -1 if x is not in A[left...right]) int bin_search(int x, const vector<int>& A, int left, int right) { while (left <= right) { int i = (left + right)/2; if (x < A[i]) right = i – 1; else if (x > A[i]) left = i + 1; else return i; //Found } return -1; } Introduction to Programming © Dept. CS, UPC

Binary search // The initial call to bin_search should // request a search in the whole array ... int i = bin_search(value, A, 0, A.size() – 1); Introduction to Programming © Dept. CS, UPC

Binary search (recursive) // Pre: A is sorted in ascending order, // 0 <= left,right < A.size() // Returns the position of x in A[left...right] // (returns -1 if x is not in A[left...right]) int bin_search(int x, const vector<int>& A, int left, int right) { if (left > right) return -1; else { int i = (left + right)/2; if (x < A[i]) return bin_search(x,A,left,i-1); else if (x > A[i]) return bin_search(x,A,i+1,right); else return i; // found } } Introduction to Programming © Dept. CS, UPC

Summary Searching is one of the most often executed operations in data structures. Efficient algorithms exist when data structures are sorted. The runtime difference between linear and logarithmic algorithms is enormous. Introduction to Programming © Dept. CS, UPC