1 Linear and Binary Search Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
CSE Lecture 3 – Algorithms I
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
1 Selection Sort and Quick Sort Instructor: Mainak Chaudhuri
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Binary Search Visualization i j.
Arrays Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Lecture 8 Analysis of Recursive Algorithms King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 07 / 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.
J. Michael Moore Searching & Sorting CSCE 110. J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a.
Searching Arrays Linear search Binary search small arrays
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 Graphs and Search Trees Instructor: Mainak Chaudhuri
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
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,
COSC 2006 Data Structures I Recursion II
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
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.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
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.
CSC 205 Java Programming II Algorithm Efficiency.
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[],
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.
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
1 Conditionals Instructor: Mainak Chaudhuri
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
1 Operators and Expressions Instructor: Mainak Chaudhuri
1 Improving search performance: Hash tables Instructor: Mainak Chaudhuri
1 Examples of Recursion Instructor: Mainak Chaudhuri
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
1 Insertion sort [ ] i=1 j=1 i=2 j=2 insert i=3 at j=1 [ ] i=3 j=1 insert i=4 at j=0 [
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
1 Examples of class: Recursive data structures Instructor: Mainak Chaudhuri
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
ARRAYS Lecture void reverse (int x[], int size) { int i; for (i=0; i< (size/2); i++) temp = x[size-i-1] ; x[size-1-1] = x[i] ; x[i] = temp;
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
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.
Unit 6 Analysis of Recursive Algorithms
Sum of natural numbers class SumOfNaturalNumbers {
More on Recursion.
RECITATION 1 ANALYSIS OF ALGORITHMS
TO COMPLETE THE FOLLOWING:
searching Concept: Linear search Binary search
Binary Search class binarysearch {
Merging two sorted arrays
Module 8 – Searching & Sorting Algorithms
Running Time Exercises
Design and Analysis of Algorithms. Presentation Topic Divide & Conquer Paradigm to design/develop algorithms.
Sorting and Searching -- Introduction
Linear and Binary Search
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Instructor: Mainak Chaudhuri
Presentation transcript:

1 Linear and Binary Search Instructor: Mainak Chaudhuri

2 Linear search class LinearAndBinarySearch { public static void main (String arg[]) { int size = 20, key = 23; int array[] = new int[size]; Initialize (array, size); // not shown PrintArray (array, size); // not shown System.out.println (“Linear search result: ” + LinearSearch (array, key)); System.out.println (“Binary search result: ” + BinarySearch (array, 0, size-1, key)); }

3 Linear search public static int LinearSearch (int array[], int key) { int i; for (i=0;i<array.length;i++) { if (array[i] == key) { return i; } return -1; }

4 Binary search public static int BinarySearch (int array[], int start, int end, int key) { // Pre-condition: array is sorted // Caution: binary search does not work // on unsorted arrays. int mid; if (start > end) return -1; if (start == end) { if (key == array[start]) return start; else return -1; } // continued in next slide

5 Binary search mid = (start + end)/2; if (key == array[mid]) return mid; else if (key < array[mid]) { return BinarySearch (array, start, mid-1, key); } else { return BinarySearch (array, mid+1, end, key); } } // end class

6 Run time analysis Linear search in the worst case requires n comparisons Binary search in the worst case requires O(log n) comparisons –Solution to T(n) = T(n/2) + O(1) –Remember that binary search can be applied to sorted arrays only