COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



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

Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
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.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
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.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
 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.
Searching Arrays Linear search Binary search small arrays
Using recursion for Searching and Sorting
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Searching.
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Recitation 13 Searching and Sorting.
Chapter 7 Single-Dimensional Arrays
COMPUTER 2430 Object Oriented Programming and Data Structures I
Searching CSCE 121 J. Michael Moore.
Adapted from Pearson Education, Inc.
Searching.
CSC212 Data Structure - Section RS
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 2430 Object Oriented Programming and Data Structures I
searching Concept: Linear search Binary search
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
COMPUTER 2430 Object Oriented Programming and Data Structures I
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Searching: linear & binary
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant.
Search,Sort,Recursion.
Searching CLRS, Sections 9.1 – 9.3.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: DataStructures Lecture 3: Searching.
Topic 24 sorting and searching arrays
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Search,Sort,Recursion.
CS 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Searching.
Searching and Sorting Arrays
CSC212 Data Structure - Section KL
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Binary Search 2

Linear Search for (int i = 0; i < size; i ++) if (a[i].equals(target)) //if (a[i] == target) return index; return -1; Order of algorithm: O(N) Have to check every element! Why? Not sorted! Quiz is coming? Program due! 3 3 3

Binary Search Sorted in Ascending Order middle middle element is larger than target Target CANNOT be in the 2nd part! Quiz is coming? Program due! middle middle element is smaller than target Target CANNOT be in the 1st part! 4 4 middle 4

Binary Search int [] a = new int[MAX_SIZE]; int size, target; // a[] is sorted in ascending order While not done Find index of the middle element If middle element = target return index else if middle element is smaller search the second half else search the first half Return -1 Quiz is coming? Program due! 5 5 5

Index of Middle 6 6 Quiz is coming? Program due! 6 lo middle = (lo+hi)/2 hi middle element is larger than target lo Quiz is coming? Program due! middle = (lo+hi)/2 hi hi = middle -1 middle element is smaller than target lo middle = (lo+hi)/2 hi lo = middle + 1 6 6 6

Index of Middle // Integer division middle = (lo + hi) / 2; middle = (0 + 51) / 2; // 25 middle = (26 + 51) / 2; // 38 middle = (26 + 37) / 2; // 31

Stop Condition While not done Find index of the middle element If middle element = target return index else if middle element is smaller search the second half else search the first half Return -1 What is the stop condition? lo < hi lo = hi lo > hi What is the while condition? !(lo > hi) lo <= hi Quiz is coming? Program due! 8 8 8

Stop Condition lo = p hi = p+1 middle = (p + (p+1)) / 2 = p middle element is larger than target middle element is smaller than target hi=p-1 lo=p Quiz is coming? Program due! lo = p+1 hi = p+1 hi < lo Done! Not Done! 9 9 9

Binary Search lo = p hi = p middle = p middle element is larger than target middle element is smaller than target hi = p-1 lo = p hi = p lo = p+1 Quiz is coming? Program due! hi < lo hi < lo Done! Done! 10 10 10

Binary Search int a[] = new int[MAX_SIZE]; int size; public int binarySearch ( int target ) { int lo = 0, hi = size - 1; while ( lo <= hi ) int middle = ( lo + hi ) / 2; if ( a[middle] == target ) return middle; else if ( a[middle] > target) hi = middle - 1; else lo = middle + 1; } return -1; Quiz is coming? Program due! 11 11 11

Big O of Binary Search How many times can N be divided in two before you get down to an array of size 1? n / 2r ≈ 1 n ≈ 2r r = log2(n) Quiz is coming? Program due! 12 12 12

Binary Search and Linear Search Linear Search Binary Search O(n) O(log n) When n = 210 1,000 10 When n = 220 1,000,000 20 Quiz is coming? Program due! 13 13 13

Search Array of Objects public interface Comparable <E> { public int compareTo ( E x ); } Return value: negative if (this) less than x 0 if (this) equals x positive if (this) greater than x

Class Date public class Date implements Comparable <Date> { . . . @Override public int compareTo(Date d) if (year == d.year && month == d.month && day == d.day) return 0; else if ((year < d.year) || (year == d.year && month < d.month) || (year == d.year && month == d.month && day < d.day)) return -1; else return 1; }

Class DateList public class DateList { private int count; private Date[] list; public DateList(int listSize) list = new Date[listSize]; count = 0; } . . . Quiz is coming? Program due!

Class DateList public class DateList { . . . // Insertion in sorted order // Assuming list size is large enough public void insert(Date d) int index = count; while (index > 0 && list[index - 1].compareTo(d) > 0) list[index] = list[index - 1]; index --; } list[index] = d; count ++; Quiz is coming? Program due!

Class DateList public class DateList { . . . // What is the Big O? // O(N) public void insert(Date d) int index = count; while (index > 0 && list[index - 1].compareTo(d) > 0) list[index] = list[-- index]; } list[index] = d; count ++; Quiz is coming? Program due!

Class DateList public class DateList { . . . // What is the Big O? // O(N) public boolean remove(Date d) int index = BinarySearch(d); if (index >= 0) count --; for (int i = index; i < count; i ++) list[i] = list[i + 1]; return true; } return false; Quiz is coming? Program due!

Class DateList public class DateList { private int count; private Date[] list; . . . public int BinarySearch(Date target) int lo = 0, hi = count - 1; while ( lo <= hi ) int mid = ( lo + hi ) / 2; int result = list[mid].compareTo(target); if ( result == 0 ) return mid; if ( result > 0 ) hi = mid - 1; else lo = mid + 1; } return -1; Quiz is coming? Program due!

public class SortedList { private Comparable [] items; private int count; public int BinarySearch(Comparable x) int lo = 0, hi = count - 1; while ( lo <= hi ) int mid = ( lo + hi ) / 2; int result = items[mid].CompareTo(x); if ( result == 0 ) return mid; if ( result > 0 ) hi = mid - 1; else lo = mid + 1; } return -1; . . . ..... Quiz is coming? Program due!

Tracing Binary Search Array items[] has 9 values and sorted in ascending order. Binary search is used to find a target. Tracing the execution assuming the target is larger than all values in the array. low mid high 0 8 4 5 6 7 8 9 Number of comparisons: 4 Quiz is coming? Program due! 22 22 22

Array items[] has 100 values and sorted in ascending order Array items[] has 100 values and sorted in ascending order. Binary search is used to find a target. Tracing the execution assuming the target is not in the array and its value is between items[40] and items[41]. low mid high 0 99 49 48 24 25 36 37 42 41 39 40 Number of comparisons: 7 Quiz is coming? Program due! 23 23 23

Due 11 pm, Monday Grace 11 pm, Wednesday (-5) Prog 5 Due 11 pm, Monday Grace 11 pm, Wednesday (-5)

Prog 6 Game Group sign up Plan in SE Tool Friday, Nov 30, by 11:50 am Could be the same as for Prog5 Plan in SE Tool Monday, Dec 3, by 11 p.m.

Lab 12 Due 11 pm, Monday, Dec 3 Must do it before Prog 6 Group assignment Same groups as for Prog6

Counting Exercise for I = (N - 1) down to 3 for J = (I + 1) to (N + 2) if ---- The total number of times the “if” is executed is: ____________ Pass I First of J Last of J Times of “if” executed N-1 N-2 N-3 . 5 4 3 N N-1 N-2 . 6 5 4 N+2 . 3 4 5 . N-3 N-2 N-1

Counting Exercise 3 + 4 + 5 + . . . + (N-3) + (N-2) + (N-1) = (Last + First) * (Number of terms) / 2 = (N+2) * ((N-1) – 3 + 1) / 2 = (N+2) * (N-3) / 2 = (N2 – N – 6) / 2

Quiz 5 Wednesday, Nov 28

Binary Search Exercise Turn in at the beginning of class Friday, Nov 30