Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CSE Lecture 3 – Algorithms I
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP T1.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
Introduction to Analysing Costs 2015-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Array operations II manipulating arrays and measuring performance.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
FASTER SORTING using RECURSION : QUICKSORT COMP 103.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Lecture 12. Searching Algorithms and its analysis 1.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Analysing Costs: ArraySet Binary Search COMP 103.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 9: Searching Data Structures.
CSC 211 Data Structures Lecture 13
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
More about costs: cost of “ensureCapacity”, cost of ArraySet, Binary Search 2014-T2 Lecture 12 School of Engineering and Computer Science, Victoria University.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
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.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103.
FASTER SORT using RECURSION : MERGE SORT COMP 103.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Chapter 16: Searching, Sorting, and the vector Type.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
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.
Searching and Sorting Searching algorithms with simple arrays
Data Structures I (CPCS-204)
Recitation 13 Searching and Sorting.
Efficiency of in Binary Trees
FASTER SORT using RECURSION : MERGE SORT
Data Structures and Algorithms for Information Processing
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
Binary Search Trees (I)
More complexity analysis & Binary Search
Algorithm design and Analysis
searching Concept: Linear search Binary search
CSE 373 Data Structures and Algorithms
CSC 143 Binary Search Trees.
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search COMP 103 #

© Peter Andreae COMP :2 Menu Binary Search SortedArraySet: Implementing Set with Binary Search Recursion Notes:

© Peter Andreae COMP :3 ArraySet algorithms Contains(value): search through array, if value equals item return true return false Add(value): if not contains(value), place value at end, (doubling array if necessary) increment size Remove(value): search through array if value equals item replace item by item at end. decrement size return Costs? How can we speed up the search?

© Peter Andreae COMP :4 ArraySet costs Costs: contains:O(n) add, O(n) + O(1) = O(n) remove:O(n) + O(1) = O(n) Question: How can we speed up the search?

© Peter Andreae COMP :5 Making ArraySet faster. Binary Search: Finding “Eel” If the items are sorted (“ordered”), then we can search fast Look in the middle: if item is middle item⇒ return if item is before middle item ⇒ look in left half if item is after middle item⇒ look in right half AntBeeCatDogEelFosGnuPig 8

© Peter Andreae COMP :6 Binary Search private boolean contains(Object item){ Comparable value = (Comparable ) item; int low = 0; // min possible index of item int high = count-1; // max possible index of item // item in [low.. high] (if present) while (low <= high){ int mid = (low + high) / 2; int comp = value.compareTo(data[mid]); if (comp == 0)// item is present return true; if (comp < 0)// item in [low.. mid-1] high = mid - 1; // item in [low.. high] else // item in [mid+1.. high] low = mid + 1;// item in [low.. high] } return false; // item in [low.. high] and low > high, // therefore item not present }

© Peter Andreae COMP :7 Another form of Binary Search /* Return the index of where the item ought to be, whether present or not. (!) */ private int findIndex(Object item){ Comparable value = (Comparable ) item; int low = 0; int high = count; // index in [low.. high] while (low 0)// index in [mid+1.. high] low = mid + 1; // index in [low.. high] low <= high else // index in [low.. mid] high = mid; // index in [low.. high], low<=high } return low; // index in [low.. high] and low = high // therefore index = low } Note: correct position might be at end (index =count)

© Peter Andreae COMP :8 Binary Search: Cost What is the cost of searching if n items in set? key step = ? Iteration Size of range Cost of iteration 1n 2 k

© Peter Andreae COMP :9 log 2 (n ) or lg(n ): The number of times you can divide a set of n things in half. lg(1000) ≈10, lg(1,000,000) ≈ 20, lg(1,000,000,000) ≈ 30 Every time you double n, you add one lg(n) Arises all over the place in analysing algorithms Especially “Divide and Conquer” algorithms: Problem Solution Solve

© Peter Andreae COMP :10 SortedArraySet algorithms Contains(value): index = findIndex(value), return (data[index] equals value ) Add(value): index = findIndex(value), if data[index] not equal to value, ensure capacity move items up, from position count-1 down to index insert value at index increment count Remove(value): index = findIndex(value), if data[index] equal to value move items down, from position index+1 to count-1 decrement count Assumes that the items in data are sorted.

© Peter Andreae COMP :11 ArraySet with Binary Search ArraySet: unordered All cost in the searching: O(n) contains:O(n ) add: O(n ) + O(1) remove:O(n ) + O(1) SortedArraySet: with Binary Search Binary Search is fast: O(log(n )) contains:O(log(n )) add: O(log(n )) O(n ) Best case? remove:O(log(n ))O(n ) All the cost is in keeping it sorted!!!!

© Peter Andreae COMP :12 Making SortedArraySet fast If you have to call add() and/or remove() many times, then SortedArraySet is no better than ArraySet! Both O(n ) Eitherpay to search Or pay to keep it in order If you only have to construct the set once, and then many calls to contains(), then SortedArraySet is much better than ArraySet. SortedArraySet contains() is O(log(n )) But, how do you construct the set fast? Adding each item, one at a time ⇒ Need a separate constructor.

© Peter Andreae COMP :13 Additional Constuctor Sort the items all at once public SortedArraySet(Collection col){ // Make space count=col.size(); data = (E[]) new Object[count]; // Put items from collection into the data array. col.toArray(data); // or count=0; for (E item : col) {data[count++]=item;} // sort the data array. Arrays.sort(temp); How do you sort? like Collections.sort(myList) but for arrays.

© Peter Andreae COMP :14 Cost of not Sorting? Constructing a sorted array one item at a time is very slow : add(item) is n/2 on average[ O(n) ] ⇒ 1/2 + 2/2 + 3/2 + …. n/2 is n 2 /4[ O(n 2 )]  2,500,000,000,000,000 steps for 100,000,000 items ⇒ 25,000,000 seconds = 289 days at 10nS per step. There are sorting algorithms that are much faster if you can sort whole array at once: O(n log(n))  2,700,000,000 steps for 100,000,000 items ⇒ 27 seconds at 10nS per step. If items are already sorted, cost of adding one at a time is….