SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

CSE Lecture 3 – Algorithms I
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CS4413 Divide-and-Conquer
HST 952 Computing for Biomedical Scientists Lecture 9.
Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
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.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Chapter 8 ARRAYS Continued
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
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.
Searching and Sorting Topics Sequential Search on an Unordered File
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
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.
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.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
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.
Searching Topics Sequential Search Binary Search.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 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)
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Searching and Sorting Arrays
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Introduction to Search Algorithms
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Chapter 8 – Searching and Sorting Arrays
Searching and Sorting Topics Sequential Search on an Unordered File
24 Searching and Sorting.
Chapter 4.
Searching and Sorting Arrays
Searching and Sorting Arrays
Presentation transcript:

SEARCHING UNIT II

Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances recursively 3.Obtain solution to original (larger) instance by combining these solutions

Divide-and-conquer technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to the original problem a solution to subproblem 2 a problem of size n

Examples Binary Search Merge Sort Quick Sort Median and Order Statistics FFT

Search Algorithms Search: A search algorithm is a method of locating a specific item of information in a larger collection of data. There are two primary algorithms used for searching the contents of an array: – Linear or Sequential Search – Binary Search

Linear Search This is a very simple algorithm. It uses a loop to sequentially step through an array, starting with the first element. It compares each element with the value being searched for (key) and stops when that value is found or the end of the array is reached.

Linear Search Algorithm pseudocode: set found to false; set position to –1; set index to 0 while index < number of elements and found is false if list[index] ==search value found = true position = index end if add 1 to index end while return position

Linear Search Function Linear searching is easy to program int itemInArray(char item, char[] arr, int validEntries) { int index = -1; for (int i = 0; i < validEntries; i++) { if (item == arr[i]) index = i; } return index; }

Linear Search Example Array numlist contains: Searching for the value 11, linear search examines 17, 23, 5, and 11 (Successful Search) Searching for the value 7, linear search examines 17, 23, 5, 11, 2, 29, and 3 (Unsuccessful Search)

Linear Search Tradeoffs Benefits: – Easy algorithm to understand – Array can be in any order Disadvantages: – Inefficient (slow): for array of N elements, examines N/2 elements on average for value in array, – N elements for value not in array

Binary Search Requires array elements to be in order 1.Divides the array into three sections: – middle element – elements on left side of the middle element – elements on the right side of the middle element 2.If the middle element is the correct value, done. Otherwise, go to step 1 using only the half of the array that may contain the correct value. 3.Continue steps 1 and 2 until either the value is found or there are no more elements to examine

Binary Search Example Array numlist2 contains: Searching for the value 11, binary search examines 11 and stops (Successful Search) Searching for the value 7, linear search examines 11, 3, 5, and stops (Unsuccessful Search)

How a Binary Search Works Always look at the center value. Each time you get to discard half of the remaining list. Is this fast ?

Binary Search Program /* This program demonstrates the binarySearch function, that performs a binary search on an integer array. */ #include /* Function prototype */ int binarySearch(int [], int, int); /* constant for array size */ const int arrSize = 20; declarations

Binary Search Program int main(void) { int tests[] = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600}; int results, empID; printf("Enter the Employee ID you wish to search for: "); scanf(“%d”, &empID); results = binarySearch(tests, arrSize, empID); if (results == -1) printf("That number does not exist in the array.\n“); else { printf("That ID is found at element %d“, results); printf(" in the array\n“); } return 0; } main function

Binary Search Program int binarySearch(int array[], int numElems, int value) { int first = 0, last = numElems - 1, middle, position = -1; int found = 0; while (!found && first <= last) {middle = (first + last) / 2; /* Calculate mid point */ if (array[middle] == value) /* If value is found at mid */ { found = 1; position = middle; } else if (array[middle] > value) /* If value is in lower half */ last = middle - 1; else first = middle + 1; /* If value is in upper half */ } return position; } search function

How Fast is a Binary Search? Worst case: 11 items in the list took 4 tries How about the worst case for a list with 32 items ?  1st try - list has 16 items  2nd try - list has 8 items  3rd try - list has 4 items  4th try - list has 2 items  5th try - list has 1 item

How Fast is a Binary Search? List has 250 items 1st try items 2nd try - 63 items 3rd try - 32 items 4th try - 16 items 5th try - 8 items 6th try - 4 items 7th try - 2 items 8th try - 1 item List has 512 items 1st try items 2nd try items 3rd try - 64 items 4th try - 32 items 5th try - 16 items 6th try - 8 items 7th try - 4 items 8th try - 2 items 9th try - 1 item

What’s the Pattern? List of 11 took 4 tries List of 32 took 5 tries List of 250 took 8 tries List of 512 took 9 tries 32 = 2 5 and 512 = < 11 < < 11 < < 250 < < 250 < 2 8

A Very Fast Algorithm! How long (worst case) will it take to find an item in a list 30,000 items long? 2 10 = = = = = = So, it will take only 15 tries!

Log 2 (n) Efficiency We say that the binary search algorithm runs in log 2 (n) time. – also written as lg(n) Log 2 (n) means the log to the base 2 of some value of n. 8 = 2 3 log 2 (8) = 316 = 2 4 log 2 (16) = 4 There are no algorithms that run faster than log 2 (n) time.

Binary Search Tradeoffs Benefits: – Much more efficient than linear search. For array of n elements, performs at most log 2 (n) comparisons Disadvantages: – Requires that array elements be sorted