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.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Data Structures Review Session 1
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
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 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
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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,
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.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
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.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.:
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.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
1 Searching and Sorting Linear Search Binary Search.
Searching CS 105 See Section 14.6 of Horstmann text.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
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.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
CSC 211 Data Structures Lecture 13
Data Structure Introduction.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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 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.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
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.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Searching CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
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 Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
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.
Search Algorithms Sequential Search (Linear Search) Binary Search
Searching.
Searching CLRS, Sections 9.1 – 9.3.
Data Structures Sorted Arrays
Data Structures: Searching
Searching.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

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 of information Searching implies that items can be compared Ideal search algorithm is O(1) – Is it possible? 2

Searching Usually want more than just a boolean result We have an element to remove, add or update If the item is found, should return the location of the search item If not found, should return location where it belongs (if list is ordered) How should duplicates be handled? 3

Linear Search Suppose you want to find a number in an unordered sequence You have no choice – look through all elements until you have found a match This is called linear or sequential search Example: find the number 4 in the array

Complexity 5 Best Case- target in first location O(1) Worst Case- O(n) Average Case- O(n) Linear search is an O(n) algorithm

Code int search(int a[], int size, int v) { for( int i = 0; i < size; i++ ) { if( a[i] == v ) return i; } return -1; } 6

Linear Search What if you do a linear search on a sorted array? Not much changes – Best case: O(1) – Worst case: O(n) – Average case: O(n) It’s still O(n). But there is a better search technique called binary search!

Binary Search What if you’re looking for 15 in this array: We’ll look at the first or last half. The number in the middle of the entire sorted array is 9. (Note: we’ll consider the middle to be the last number in the half). 9 < 15, so 15 must be in the second half. We’ll grey out the first half

Binary Search 9 The number in the middle of the right half is < 17, so 15 The number in the middle of the left half is < 15, so 15 must be in the right half (of the white numbers)

Binary Search 17 is obviously not 15. So 15 is not in the array. However, if we had to insert 15 into the array, we would put it before 17 at index 5 What we just did is called binary search. – It’s binary because we cut the size of the search in half at each step. – This cutting in half only works because the array is sorted

Code int search(int a[], int size, int v) { int low = 0; int high = size – 1; while( low <= high ) { int mid = (low + high)/2; int diff = a[mid] – v; if( diff == 0 ) // a[mid] == v return mid; else if (diff < 0) // a[mid] < v low = mid + 1; else high = mid – 1; } return -1; } 11

Complexity 12 The size of the search space is reduced by 2 every iteration That makes the complexity O(log n) Best Case- O(1) Worst Case- O(log n) Average Case- O(log n)

Choosing a Search Method Linear search is O(n) Binary search is O(log n) Linear search is easier to code Binary search will be MUCH faster on large data – But the data must be sorted first 13

Tradeoff Is it faster to: a) Sort an array first then use binary search OR b) Do a linear search on the unsorted array? It depends… If you search only once, then it’s better to do a linear search, which is O(n) If you search many times, then it’s better to pay the price of sorting, which is O(n log(n)) then do as many O(log(n)) binary searches as you have to do 14