Searching.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Searching and Sorting I 1 Searching and Sorting 1.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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/Sorting Introduction to Computing Science and Programming I.
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 Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Asymptotic Notations Iterative Algorithms and their analysis
Building Java Programs Chapter 13 Searching reading: 13.3.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
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 Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
RECURSION Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
The Linear and Binary Search and more Lecture Notes 9.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
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
Using recursion for Searching and Sorting
16 Searching and Sorting.
Searching.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Recitation 13 Searching and Sorting.
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Recursive Binary Search
Quicksort 1.
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
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.
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
Algorithm design and Analysis
Searching.
Announcements P2 is due tomorrow Prelim on Monday
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
Chapter 8 Search and Sort
Lecture 11 Searching and Sorting Richard Gesick.
Lecture 15: binary search reading:
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
CSE 373 Data Structures and Algorithms
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
Chapter 4.
Basics of Recursion Programming with Recursion
Search algorithms for vectors
Building Java Programs
Quicksort.
Searching.
Building Java Programs
Quicksort.
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
Module 8 – Searching & Sorting Algorithms
Quicksort.
Presentation transcript:

Searching

About sorted arrays An array is sorted in ascending order if each element is no smaller than the preceding element An array is sorted in descending order if each element is no larger than the preceding element When we just say an array is “sorted,” we usually mean that it is sorted in ascending order

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 int linearSearch(int target, int[] a) { for (int i = 0; i < a.length; i++) { if (target == a[i]) return i; } return -1; // not a legal index }

Linear search takes linear time Linear search has linear time complexity: If the target (the thing we are looking for) is not in the array, we have to look at all n elements If the target is in the array, we have to look at n/2 elements on average If we search an array that is twice as large, we have to look at twice as many elements For some constants k1 and k2, the time required to search the array is T = k1 * n + k2 for an array of size n This is a linear equation

Binary search How do we look up a name in a phone book, or a word in a dictionary? Look somewhere in the middle Compare what’s there with the thing you’re looking for Decide which half of the remaining entries to look at next Repeat until you find the correct place This is the binary search algorithm

Example of binary search 5 7 10 13 15 19 23 28 32 37 41 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Search the following array a for 36: a 46 4. (12+12)/2=13; a[13]=32; too small; search 13..12 3. (12+15)/2=13; a[13]=37; too large; search 12..12 2. (8+15)/2=11; a[11]=32; too small; search 12..15 1. (0+15)/2=7; a[7]=19; too small; search 8..15 ...but 13>12, so quit: 36 not found

Binary search in Java static int binarySearch(int target, int[] a, int left, int right) { while (left <= right) { int middle = (left + right) / 2; if (a[middle] == target) return middle ; // found else if (a[middle] > target) right = middle – 1; else left = middle + 1; } return -1; // -1 means "not found"

Recursive binary search in Java static int binarySearch(int target, int[] a, int left, int right) { if (left > right) return -1; // not found int middle = (left + right) / 2; if (a[middle] == target) return middle ; // found else if (a[middle] > target) return binarySearch(target, a, left, middle - 1); else return binarySearch(target, a, middle + 1, right); }

An aside: Style To do a binary search for n in array a, anyone who wants to use our binary search routines would have to write result = binarySearch(n, a, 0, a.length); The last two parameters are not something the user cares about, so it is poor style to require them These parameters can easily be removed from the iterative version of binary search They cannot be removed from the recursive version Solution: Write an adapter method: static int binarySearch(int target, int[] a) { return binarySearch(target, a, 0, a.length); }

Binary search takes log n time In binary search, we choose an index that cuts the remaining portion of the array in half We repeat this until we either find the value we are looking for, or we reach a subarray of size 1 If we start with an array of size n, we can cut it in half log2n times Hence, binary search has logarithmic (log n) time complexity For an array of size 1000, this is 100 times faster than linear search (210 ~= 1000)

Conclusion Linear search has linear time complexity Binary search has logarithmic time complexity For large arrays, binary search is far more efficient than linear search However, binary search requires that the array be sorted If the array is sorted, binary search is 100 times faster for an array of size 1000 50 000 times faster for an array of size 1 000 000 This is the kind of speedup that we care about when we analyze algorithms

The End