Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search."— Presentation transcript:

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

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

3 © Peter Andreae COMP 103 13: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?

4 © Peter Andreae COMP 103 13: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?

5 © Peter Andreae COMP 103 13: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 012345678 AntBeeCatDogEelFosGnuPig 8

6 © Peter Andreae COMP 103 13: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 }

7 © Peter Andreae COMP 103 13: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)

8 © Peter Andreae COMP 103 13: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 k1 012345678929101112133014151617181920212223242526272831

9 © Peter Andreae COMP 103 13: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

10 © Peter Andreae COMP 103 13: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.

11 © Peter Andreae COMP 103 13: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!!!!

12 © Peter Andreae COMP 103 13: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.

13 © Peter Andreae COMP 103 13: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.

14 © Peter Andreae COMP 103 13: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….


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

Similar presentations


Ads by Google