1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p.671-679.

Slides:



Advertisements
Similar presentations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advertisements

Searching and Sorting Linear Search Binary Search Selection Sort
Recursion. Binary search example postponed to end of lecture.
Recursion Chapter 11 Chapter 11.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Recursion.
Chapter 11 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Searching Arrays Linear search Binary search small arrays
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
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.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
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.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
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.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
CSC 211 Data Structures Lecture 13
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Recursion.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
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.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 11 Recursion Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
CIS Principles of Programming
Searching.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Searching and Sorting Linear Search Binary Search ; Reading p
MSIS 655 Advanced Business Applications Programming
CSE 373 Data Structures and Algorithms
Programming with Recursion
Comp 249 Programming Methodology
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p

2 © 2006 Pearson Addison-Wesley. All rights reserved Linear Search Searching is the process of determining whether or not a given value exists in a data structure or a storage media. We discuss two searching methods on one-dimensional arrays: linear search and binary search. The linear (or sequential) search algorithm on an array is: –Sequentially scan the array, comparing each array item with the searched value. –If a match is found; return the index of the matched element; otherwise return –1. Note: linear search can be applied to both sorted and unsorted arrays.

3 © 2006 Pearson Addison-Wesley. All rights reserved Linear Search The algorithm translates to the following Java method: public static int linearSearch(Object[] array, Object key) { for(int k = 0; k < array.length; k++) if(array[k].equals(key)) return k; return -1; }

4 © 2006 Pearson Addison-Wesley. All rights reserved Binary Search Binary search uses a recursive method to search an array to find a specified value The array must be a sorted array: a[0]≤a[1]≤a[2]≤... ≤ a[finalIndex] If the value is found, its index is returned If the value is not found, -1 is returned Note: Each execution of the recursive method reduces the search space by about a half

5 © 2006 Pearson Addison-Wesley. All rights reserved Binary Search An algorithm to solve this task looks at the middle of the array or array segment first If the value looked for is smaller than the value in the middle of the array –Then the second half of the array or array segment can be ignored –This strategy is then applied to the first half of the array or array segment

6 © 2006 Pearson Addison-Wesley. All rights reserved Binary Search If the value looked for is larger than the value in the middle of the array or array segment –Then the first half of the array or array segment can be ignored –This strategy is then applied to the second half of the array or array segment If the value looked for is at the middle of the array or array segment, then it has been found If the entire array (or array segment) has been searched in this way without finding the value, then it is not in the array

7 © 2006 Pearson Addison-Wesley. All rights reserved Pseudocode for Binary Search

8 © 2006 Pearson Addison-Wesley. All rights reserved Recursive Method for Binary Search

9 © 2006 Pearson Addison-Wesley. All rights reserved Execution of the Method search (Part 1 of 2)

10 © 2006 Pearson Addison-Wesley. All rights reserved Execution of the Method search (Part 1 of 2)

11 © 2006 Pearson Addison-Wesley. All rights reserved Checking the search Method 1.There is no infinite recursion On each recursive call, the value of first is increased, or the value of last is decreased If the chain of recursive calls does not end in some other way, then eventually the method will be called with first larger than last

12 © 2006 Pearson Addison-Wesley. All rights reserved Checking the search Method 2.Each stopping case performs the correct action for that case If first > last, there are no array elements between a[first] and a[last], so key is not in this segment of the array, and result is correctly set to - 1 If key == a[mid], result is correctly set to mid

13 © 2006 Pearson Addison-Wesley. All rights reserved Checking the search Method 3.For each of the cases that involve recursion, if all recursive calls perform their actions correctly, then the entire case performs correctly If key < a[mid], then key must be one of the elements a[first] through a[mid-1], or it is not in the array The method should then search only those elements, which it does The recursive call is correct, therefore the entire action is correct

14 © 2006 Pearson Addison-Wesley. All rights reserved Checking the search Method If key > a[mid], then key must be one of the elements a[mid+1] through a[last], or it is not in the array The method should then search only those elements, which it does The recursive call is correct, therefore the entire action is correct The method search passes all three tests: Therefore, it is a good recursive method definition

15 © 2006 Pearson Addison-Wesley. All rights reserved Efficiency of Binary Search The binary search algorithm is extremely fast compared to an algorithm that tries all array elements in order –About half the array is eliminated from consideration right at the start –Then a quarter of the array, then an eighth of the array, and so forth

16 © 2006 Pearson Addison-Wesley. All rights reserved Efficiency of Binary Search Given an array with 1,000 elements, the binary search will only need to compare about 10 array elements to the key value, as compared to an average of 500 for a serial search algorithm The binary search algorithm has a worst-case running time that is logarithmic: O(log n) –A serial search algorithm is linear: O(n) If desired, the recursive version of the method search can be converted to an iterative version that will run more efficiently

17 © 2006 Pearson Addison-Wesley. All rights reserved Iterative Version of Binary Search (Part 1 of 2)

18 © 2006 Pearson Addison-Wesley. All rights reserved Iterative Version of Binary Search (Part 2 of 2)