searching Concept: Linear search Binary search

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
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.
Chapter 8 ARRAYS Continued
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
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.
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.
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 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular 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.
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.
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]
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Tigbur 16.1 Complexity Sorting. Complexity סימון אסימפטוטי.
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 Searching algorithms with simple arrays
Searching and Sorting Arrays
Searching.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Searching Given a collection and an element (key) to find… Output
Lecture 14 Searching and Sorting Richard Gesick.
Recitation 13 Searching and Sorting.
Searching an Array: Binary Search
Data Structures and Algorithms for Information Processing
Chapter 7 Single-Dimensional Arrays
Sorting and Searching Sudeshna Sarkar 7th Feb 2017.
Search Algorithms Sequential Search (Linear Search) Binary Search
Topic 14 Searching and Simple Sorts
Adapted from Pearson Education, Inc.
Chapter 18-3 Recursion Dale/Weems.
Outline Late Binding Polymorphism via Inheritance
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Topic 1: Problem Solving
Lecture Set 9 Arrays, Collections and Repetition
Linear Search Binary Search Tree
Topic 14 Searching and Simple Sorts
Topic 24 sorting and searching arrays
COMPUTER 2430 Object Oriented Programming and Data Structures I
Given value and sorted array, find index.
CS 2430 Object Oriented Programming and Data Structures I
Linear Search (Area Code Example)
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Binary Search Counting
Sorting and Searching -- Introduction
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Chapter 11 Sorting and Searching
Presentation transcript:

searching Concept: Linear search Binary search We have a list or array of items We want to determine if a particular key is in the list Linear search Search items, one by one On average each search considers half of the list Binary search The list must be in sorted Each search requires log2 list size compares

Linear search code private boolean linearSearch (Comparable[] data, Comparable which) { for (int i=0; i<data.length; i++) if (data[i].compareTo(which)==0) return true; } return false;

Binary Search code private boolean binarySearch(Comparable[] data, Comparable which) { int low=-1, high = data.length, middle, compare; while (low+1<high) middle = (low + high)/2; compare = data[middle].compareTo(which); if (compare==0) return true; else if (compare<0) low = middle; else high =middle; } return false;

Generic search code Linear Search private boolean genericSearch(ArrayList dataList, Comparable which) { return (dataList.contains(which)) ? true: false; } Binary Search return (Collections.binarySearch(dataList, which)>=0)? true: false;