SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.

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

MATH 224 – Discrete Mathematics
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
HST 952 Computing for Biomedical Scientists Lecture 9.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
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.
Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 8 ARRAYS Continued
COMP s1 Computing 2 Complexity
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
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.
EENG212 Algorithms and Data Structures
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
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.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
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 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.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
February 4, 2005 Searching and Sorting Arrays. Searching.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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[],
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.
CSCI 130 Array Searching. Methods of searching arrays Linear –sequential - one at the time –set does not have to be ordered Binary –continuously cutting.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
1 Lower Bound on Comparison-based Search We have now covered lots of searching methods –Contiguous Data (Arrays) Sequential search Binary Search –Dynamic.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
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.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 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.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Searching Arrays Linear search Binary search small arrays
Using recursion for Searching and Sorting
Searching Given a collection and an element (key) to find… Output
Lecture 14 Searching and Sorting Richard Gesick.
And now for something completely different . . .
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
24 Searching and Sorting.
Presentation transcript:

SEARCHING (Linear/Binary)

Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search algorithms: –Linear Search –Binary Search:  LINEAR SEARCH –uses a loop to sequentially step through the array, starting at front and comparing items –if reach the end and that item does not match, the item is not found –ALGORITHM: Set position number to 0 For each array element: –Compare item to array element at position number –if match return the subscript –Move to next element

LINEAR SEARCH (cont.)  Code: int LinearSearch(int a[], int listSize, int item) { for (int pos=0; pos < listSize; pos++) { if (a[pos] = = item) return pos; } return -1; } QUESTION: Why -1 returned?

SAMPLE CODE (LINEAR SEARCH) void main() { int MyArray = {87, 85, 77, 100, 82}; int itemtofind; cout << "Enter an array item to look for:"; cin >> itemtofind; int found = LinearSearch(MyArray, 5, itemtofind); if (found == -1) cout << "That item is not in the array!"; else cout << itemtofind << " is located at position " << found; } [trace this]

Advantages/Disadvantages of Linear Search: Advantages: easy to understand and implement Disadvantages: not efficient (what if item is last one?) in average case, n/2 comparisons will be made in worst case, n comparision will be made thus, time complexity of Linear Search: O(n)

BINARY SEARCH –more efficient than linear search; –recursive algorithm keep dividing array in half, and checking half of it for item if get down to 1 element, and that doesn't match, them item not there ALGORITHM: –Set first to 0 –Set last to last subscript –Set Found to False –Set middle to middle element of current list –if middle matches the item return middle –else if middle > then item set last to middle - 1 Call binary search with new last –else set first to middle + 1 call binary search with new first

CODE FOR BINARY SEARCH int BinarySearch(int a[], int first, int last, int item) { if (last < First) return -1; else { int middle = (last + first) / 2; if (item == a[middle]) return middle; else if (a[middle] > item) return BinarySearch(a,first,middle-1,item); else return BinarySearch(a,middle+1,last,item); }

CODE USING BINARY SEARCH void main() { int MyArray={101, 114, 150, 190, 180, 266, 274, 383}; int itemtofind; cout << "Enter an item to search for:"; cin >> itemtofind; int foundit = BinarySearch(MyArray, 0, 7, itemtofind); if (foundit == -1) cout << "That item is not in list" << endl; else cout << "The item is located at position " << itemtofind; }

BINARY SEARCH SUMMARY list is halved each time BS is called maximum number of comparisons: 1) if n = 1, algorithm invoked 2 times 2) if n > 1, algorithm invoked 2 m times where m is size of sequence being searched 3) thus, total number of invocations: a n = 1 + a n/2 which is called the recurrance relation 4) Thus, by solving the recurrance relation, we get: a 2k = 1 + a 2k-1 2 k-1 < n <= 2 k k-1 < log n <= k so, Algorithm complexity: O(log n) [see discrete textbook for more details]

QUESTIONS? Read Running Time Section (302 – 312)