ECE 122. Engineering Problem Solving Using Java

Similar documents
Search Lesson Outline

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

Lecture 6: Divide-and-Conquer

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Sorting/Searching and File I/O. Sorting Searching Reading for this lecture: L&L

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

SORTING AND SEARCHING

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

COMP102: Introduction to Databases, 14

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

Sorting and Searching

CS 103 Unit 8b Slides

Insertion Sort: an algorithm for sorting an array

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CSc 110, Spring 2017 Lecture 39: searching

Searching and Sorting

Department of Computer Science Admission Test for PhD Program. Part I Time : 30 min Max Marks: 15

Algorithm Efficiency, Big O Notation, and Javadoc

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Binary Search and Worst-Case Analysis

Binary Search and Worst-Case Analysis

Intro to Algorithms. Professor Kevin Gold

Data Structures Lecture 3 Order Notation and Recursion

COMP171. Hashing.

Recursion: The Beginning

Questions. 6. Suppose we were to define a hash code on strings s by:

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting

Algorithm Analysis. Performance Factors

Binary Search APRIL 25 TH, 2014

AQA Computer Science A-Level Searching algorithms Advanced Notes

8. Binary Search Tree

CS 103 Lecture 4 Slides

Fun facts about recursion

Introduction to the Analysis of Algorithms. Algorithm

Principles of Algorithm Analysis. Biostatistics 615/815

Searching, Sorting. Arizona State University 1

Recursion: The Beginning

Lecture 6: Hashing Steven Skiena

CS 106 Introduction to Computer Science I

Chapter 12: Indexing and Hashing. Basic Concepts

Hash Table and Hashing

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Testing and Debugging

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

CSE 332: Data Structures & Parallelism Lecture 5: Algorithm Analysis II. Ruth Anderson Winter 2018

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

CMPSCI 105: Lecture #12 Searching, Sorting, Joins, and Indexing PART #1: SEARCHING AND SORTING. Linear Search. Binary Search.

Chapter 12: Indexing and Hashing

UNIT 5B Binary Search

Introduction to Computers and Programming. Today

Programming II (CS300)

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

Lecture 15: Algorithms. AP Computer Science Principles

CSE100. Advanced Data Structures. Lecture 21. (Based on Paul Kube course materials)

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

THE UNIVERSITY OF WESTERN AUSTRALIA

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

Scan and its Uses. 1 Scan. 1.1 Contraction CSE341T/CSE549T 09/17/2014. Lecture 8

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

CSE 214 Computer Science II Searching

Lecture 6 Sorting and Searching

ASYMPTOTIC COMPLEXITY

(Refer Slide Time: 00:50)

Lecture Notes on Priority Queues

Building Java Programs Chapter 13

CmpSci 187: Programming with Data Structures Spring 2015

Announcements. Reading Material. Recap. Today 9/17/17. Storage (contd. from Lecture 6)

Programming II (CS300)

Here we will only consider the problem of searching among the elements of an array. Intro Programming in C++

Searching. 11. Searching

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

CP222 Computer Science II. Searching and Sorting

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

Hashing. October 19, CMPE 250 Hashing October 19, / 25

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Hash Tables. Johns Hopkins Department of Computer Science Course : Data Structures, Professor: Greg Hager

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

(Refer Slide Time: 01.26)

Database Systems II. Record Organization

Balanced Search Trees

Chapter 3: The Efficiency of Algorithms

COS 226 Midterm Exam, Spring 2009

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

CS261 Data Structures. Ordered Bag Dynamic Array Implementa;on

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

Scan and Quicksort. 1 Scan. 1.1 Contraction CSE341T 09/20/2017. Lecture 7

Binary Search Trees. Manolis Koubarakis. Data Structures and Programming Techniques

Transcription:

ECE 122 Engineering Problem Solving Using Java Lecture 27 Linear and Binary Search

Overview Problem: How can I efficiently locate data within a data structure Searching for data is a fundamental function of computers Many different search algorithms exist The simplest ones discussed today We evaluate search algorithm based on their complexity Lowest complexity algorithm may not be best for all data sets

Observations We can search either ordered or unordered lists The unordered case is straightforward. Ordered search requires an ordered list How can data be sorted? Discussed next lecture Search applications: - search within documents, over a collection - database applications Sorting applications: - anywhere data is organized - present the results of a search (e.g. Google)

Search Algorithm A step-by-step description of how to solve the search problem Some search algorithms: sequential search binary search Linear search - a simple search algorithm Compare the target key to the key of records one by one starting from the first record Easy to implement for Array

Linear Search Brute force approach Start at the beginning and examine each element in turn. It takes linear time in both the worst and average cases. Sometimes called sequential search 6 5 4 3 2 1 21 13 8 5 3 2? target 8 How can we locate a specific element in an array?

Pseudo-code of Linear Search For Array int LinearSearch( int key, int[ ] db ) { int index; for( index=0; index<db.length; index++ ) { } } return 1; if( db[index]==key ) return index; 6 5 4 3 2 1 21 13 8 5 3 2? target 8 An Array db stores integer numbers Search for first value 8 in this array If found, return index Otherwise, return 1

Linear Search Worst case No faster than the old version. Average still in O(n) reduced by a factor of 2. Let event i be the event that the target, if it were present, would belong right before element i.

Linear Search For A Linked List Linear Search also works for Linked List An Linked List db, in which each node stores a string Searching B in this list If found, return that node Otherwise, return null

Complexity of Linear Search Suppose there are n records In the best case, the target is the first entry. It only takes 1 step In the worst case, the target is the last entry, or it cannot be found. Sequential search has to scan all n records and takes totally n steps. Cost: O(n)

Binary Search If the records are sorted by the key, we can do better. Think about how you look up a word in English dictionary where the words are sorted alphabetically. If the target is present in the list, it must be one between low and high A B C M X Y Z low=0 middle=12 high=25 Take a look at the middle one. middle = (low+high) / 2

Binary Search: Case 1 Assume target is M A B C M X Y Z low=0 middle high=25 If the target is the same as the key value in the middle position, we have found the record. Return the value of middle

Binary Search: Case 2 Assume target is E A B C L M X Y Z low=0 high=11 If target < the key value in the middle position, we know that it can not be found in the right half. We only need to search in the left half. high = middle - 1;

Binary Search: Case 3 Assume target is P A B C M N X Y Z low=13 high=25 If target > the key value in the middle position, we know that it can not be found in the left half. We only need to search the right half. low = middle + 1;

Binary Search Starting our search in the middle with a sorted array. A constant amount of work allows us to divide the data in half.

Pseudo-code of Binary Search int BinarySearch(char target, char[] array) { int low=0, high = array.length-1, middle; while (low <= high) { middle = (low+high)/2; if( array[middle]==target ) return middle; else if( array[middle]<target ) low = middle + 1; else high = middle - 1; } return -1; } Keep splitting the array in half until result is found Array must initially be ordered

Binary Search

Complexity For Binary Search Suppose there are n records In the best case, the target is the middle entry. It only takes 1 step In the worst case, it will take log 2 n + 1 steps. Cost: O(logN)

Logarithmic versus Linear How big is the practical difference?

Binary Search

Binary Search The running time of binary search is proportional to the number of times the loop runs. The number of times we have to divide the data in half before we run out of data. In the worst case, we always have to look in the larger piece. The number of passes through the loop is p + 1, where 2 p = n. If n = 8, we have one pass where there are 8 candidate elements, one where there are 4, one where there are 2, and one where there is 1. This is four passes. Notice that 2³ = 8. If n were 2 4 = 16, we would need 5 passes. O(log n)

Comparing Search Algorithms On average, a linear search would examine n/2 elements before finding the target Therefore, a linear search is O(n) The worst case for a binary search is (log 2 n) comparisons A binary search is a logarithmic algorithm It has a time complexity of O(log 2 n) But keep in mind that the search pool must be sorted For large n, a binary search is much faster

Summary Linear search is easy to implement Basically, just a for loop Has O(n) complexity for n stored items More efficient than binary search for small n Binary search harder to implement but more efficient Requires data to be sorted Has O(log n) complexity for n stored items Frequently used for reasonably-sized data sets - E.g. more than 10-20 stored items