1 2. Program Construction in Java. 2.8 Searching.

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

College of Information Technology & Design
More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
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.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
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.
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 and Sorting Arrays
Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
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.,
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
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.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
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.
Computer Science Searching & Sorting.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
1 Searching and Sorting Linear Search Binary Search.
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.
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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
CS1101: Programming Methodology Aaron Tan.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
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.
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.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. 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.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
COP 3503 FALL 2012 Shayan Javed Lecture 15
Searching CSCE 121 J. Michael Moore.
Searching and Sorting Arrays
Searching: linear & binary
Topic 1: Problem Solving
Linear Search Binary Search Tree
Presentation transcript:

1 2. Program Construction in Java

2.8 Searching

3 The need Consider such vast databases as media players (iTunes, Spotify) and geographical information systems (GES's, GoogleEarth) Getting to a particular track or location quickly is becoming increasingly important. We consider two methods of searching...

4 Linear search Basic algorithm: start at one end, pass through each element comparing with the wanted value, stop when they match (return a dummy or sentinel value if not found). This is slow with larger arrays, but is simple to program and does not depend on the array being in order.

5 Linear search ‣ int [] ages = {17,19,15,21,19,23, 14,16}; int wanted = input (“Which age are you looking for? “); for (int i=0; i<=7; i++) { if (ages[i] == wanted){ output (“Posn. “ + i); break; } output (“Value not found.“); }

6 Linear search ‣ String [] channels = {"TF1","FR2","FR3","ARTE","TV5", "M6","TLT","BBC1","CNN","TVE"}; String wanted = input (“Which channel are you looking for? “); // call the search method: output (“This channel is at position “ + linearSearch (wanted); //continued...

7 Linear search ‣ public static int linearSearch (String wanted){ for (int i=0; i<=9; i++) { if (wanted.equals(channels[i]){ return i; } } return -1; }

8 Binary search Basic algorithm: if the array is in order, split it in two, decide whether the wanted value is lower or higher than the middle, then do the same on just that half and continue until the wanted value is found (return a sentinel if not found).

9 Binary search This depends on the array being kept in order (not as uncommon as you might think). A binary search will be much faster than a linear one on large arrays. A Java implementation of the binary search is available in the project ArrayBinarySearch but will not be examined until we have studied sorting.

10 Binary search Initialise these values: ‣ // value to be returned int result = -1; // start point of list being examined int first = 0; // end point of list being examined int last = 99; // mid point of the list int mid;

11 Binary search Then enter a loop that continues while the first and loast have not collided and while the value has not been found ‣ while ((first <= last) && (result == -1)) {

12 Binary search Pick the mid point and maybe it’s your lucky day! ‣ mid = (first + last) / 2; ‣ if (wanted == numbers [mid] { result = mid; // get it's position in the array }

13 Binary search But that’s unlikely, so reduce the list by half and try again ‣ else { ‣ if (wanted < numbers[mid]) { // wanted is in lower half last = mid - 1; // adjust last } else { // it must be in the upper half first = mid + 1; // adjust first } }

14 Binary search Keep going around the loop until first > last or wanted is found, then ‣ return result;

15 Binary search [A teaser for HL candidates] Since a binary search is very repetitive (split and jump one way, split and jump one way...), it can also be handled using recursion (calling the method from inside itself) [see Recursion, HL only)