CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Searching Algorithms. The Search Problem Problem Statement: Given a set of data e.g., int [] arr = {10, 2, 7, 9, 7, 4}; and a particular value, e.g.,
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
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,
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
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.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
1 Searching and Sorting Linear Search Binary Search.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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[],
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.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSC 211 Data Structures Lecture 13
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
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 & 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.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
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
Searching and Sorting Arrays
COP 3503 FALL 2012 Shayan Javed Lecture 15
Recursive Binary Search
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Binary Search A binary search algorithm finds the position of a specified value within a sorted array. Binary search is a technique for searching an ordered.
Searching and Sorting Arrays
Linear and Binary Search
Searching.
Presentation transcript:

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never meant to know. For everything else, there’s Google! ”

Searching Querying for something Searching an array for a particular value is a common problem Example: Where is 100 in this array? A[0] A[1] A[n-1] n=10 …

Searching Querying for something Searching an array for a particular value is a common problem Example: Where is 100 in this array? A[0] A[1] A[n-1] n=10 … Must return location in the array (index=4)

The Search Problem 1.Let A be the array to be searched n - the number of elements k - the search target (or key) 2.Question: Does k occur in A? If k appears in A[0], A[1], …, A[n-1]: “found” determine its index i. that is, find i such that A[i] == k Else “not found”: return -1

Linear Search // Performs linear search of array for key, from start index to end index. int linearSearch (int[ ] A, int key, int start, int end) { for (int i = start; i < end; i++) { if (key == A[i]) { return i; // “found key”, return index } return -1; // “key not found”, return -1 }

Linear Search Advantages: easy and straightforward algorithm. array can be in any order. Disadvantages: slow and inefficient! Given an array of n elements, it examines n/2 elements on average for value in array, n elements for value not in array

Binary Search Searching a sorted array Fast and efficient

Binary Search 1. get the middle element; 2. if middle element equals searched value, then the algorithm stops (return index); 3. otherwise, two cases are possible: * searched value is less than the middle element. In this case, go to the step 1 for the part of the array before middle element. * searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array after middle element.

Binary Search A[n/2]leftright if (A[n/2] == key) return “found” else if (A[n/2] > key) search in left else search in the right

Binary Search I int binarySearch( int[] array, int key, int left, int right ) { if (left > right) return -1; // “not found” int middle = (left + right) / 2; if (array[middle] == key) return middle; // “found” else if (array[middle] > key) return binarySearch(array, key, left, middle-1); else return binarySearch(array, key, middle+1, right); } Recursion

Binary Search II int binarySearch(int[] array, int key, int left, int right){ while (left < right) { int middle = (left + right)/2; // Compute mid point if (array[mid] > key) { right = mid; // repeat search in bottom half } else if (array[mid] < key) { left = mid + 1; // Repeat search in top half } else { return mid; // “found” } return -1; // “not found” } Iteration

Binary Search Advantages: fast and efficient! Disadvantages: array elements have to be in sorted order. For an array of 1 million elements, linear search takes 500,000 comparisons to find the key, binary search takes only 20 comparisons!

Search Linear search in arbitrary array: Reduce search region by 1 in each step Binary search in sorted array: Reduce search region by half in each step