Searching CS 110: Data Structures and Algorithms First Semester, 2010-2011.

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

Introduction to Algorithms Quicksort
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
CS421 - Course Information Website Syllabus Schedule The Book:
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Efficiency of Algorithms February 11th. Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm.
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
Building Java Programs Chapter 13 Searching reading: 13.3.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
UNIT 18 Searching and Sorting.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Analysis of 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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
1 Searching and Sorting Linear Search Binary Search.
Searching CS 105 See Section 14.6 of Horstmann text.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
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 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.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
CSC 211 Data Structures Lecture 13
Data Structure Introduction.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
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.
Sorting.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Searching Given a collection and an element (key) to find… Output
CS 3343: Analysis of Algorithms
Searching CLRS, Sections 9.1 – 9.3.
Cs212: DataStructures Lecture 3: Searching.
Searching.
Presentation transcript:

Searching CS 110: Data Structures and Algorithms First Semester,

Learning Objectives ► Explain and implement search algorithms ► Explain pros and cons of each implementation

The Search Problem ► Given an array A storing n numbers, and a target number v, locate the position in A (if it exists) where A[i] = v ► Example ► Input:A = {3,8,2,15,99,52} v = 99 ► Output:position 4 ► Notes ► Array positions (indexes) go from 0..n-1 ► When the target does not exist in the array, return an undefined position, such as -1

Algorithm 1: Linear Search Algorithm LinearSearch(A,v): Input: An array A of n numbers, search target v Output: position i where A[i]=v, -1 if not found for i  0 to n - 1 do if A[i] = v then return i return -1

Linear Search Time Complexity ► Worst-case: O( n ) ► If v does not exist or if v is the last element in the array ► Best-case: O( 1 ) ► When the target element v is the first element in the array ► Average-case: O( n ) ► e.g., if the target element is somewhere in the middle of the array, the for-loop will iterate n/2 times. Still O( n )

Searching in Sorted Arrays ► Suppose the array is arranged in increasing or non-decreasing order ► Linear search on a sorted array still yields the same analysis ► O( n ) worst-case time complexity ► Can exploit sorted structure by performing binary search ► Strategy: inspect middle of the search list so that half of the list is discarded at every step

Algorithm 2: Binary Search Algorithm BinarySearch(A,v): Input: A SORTED array A of n numbers, search target v Output: position i where A[i]=v, -1 if not found low  0, high  n-1 while low <= high do mid  (low+high)/2 if A[mid]= v then return mid else if A[mid] < v then low  mid+1 else high  mid-1 return -1

Binary Search Time Complexity ► Time complexity analysis: how many iterations of the while loop? (assume worst-case) ► Observe values for low and high ► Note that before loop entry, size of search space = n = high-low+1. ► On each succeeding iteration, search space is cut in half ► Loop terminates when search space collapses to 0 or 1

Binary Search Time Complexity ►

► Worst-case: O( log n ) ► If v does not exist ► Best-case: O( 1 ) ► When the target element v happens to be in the middle of the array ► Average-case: O( log n ) ► Technically, some statistical analysis needed here (beyond the scope of this course)

Binary Search (version 2) ► Can use recursion instead of iteration ► BinSearch(A,v): return BinSearchHelper(A,0,n-1,v) ► BinSearchHelper(A,low,high,v): if low > high then return -1 mid  (low+high)/2 if A[mid] = v then return mid else if A[mid] < v then return BinSearchHelper(A,mid+1,high,v) else return BinSearchHelper(A,low,mid-1,v)

Summary ► Linear search on an array takes O( n ) time in the worst-case ► If the array is sorted, can perform binary search in O( log n ) time ► Iterative and recursive versions ► Both algorithms run in O( 1 ) time in the best-case ► Case where the first element inspected is the target element