Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorted Dynamic Array Bag and Set

Similar presentations


Presentation on theme: "Sorted Dynamic Array Bag and Set"— Presentation transcript:

1 Sorted Dynamic Array Bag and Set
CS 261 Winter 2011 Sorted Dynamic Array Bag and Set

2 The power of Ordered Collections
Why do you suppose that dictionaries or phonebooks keep their elements in order? Suppose I asked you to find the phone number for Chris Smith? Suppose I asked you who was the person with phone number ?

3 Gusss My Number We all know the heuristic of cutting a collection in half from the game “guess my number”. I’m thinking of a number between 1 and 100. What questions will you ask to find my number?

4 Binary Search The formal name for this process is Binary Search.
Works by cutting the region containing the value in half at each step. If I start out with n items, how many times can I cut in half before I reach a set of size one?

5 Log n search A log n search is much much fastar than an O(n) search.
Log of the largest integer value is still less than 32!

6 Binary Search Algorithm
int binarySearch (double * data, int size, double testValue) { int low = 0; int high = size; while (low < high) { int mid = (low + high) / 2; if (data[mid] < testValue)) low = mid + 1; else high = mid; } return low;

7 What does this return If value is found in set, returns index
If value is not in set, returns position where it can be inserted without violating ordering Note that this can be larger than a legal index

8 Makes which Bag operation faster?
If we are using a dynamic array to store values, Which of the following operations is made faster by using a binary search? Add(Element) Contains(Element) Remove(Element)

9 An example operation int sortedArrayContains
(struct dyArray *da, EleType testValue) { int index = binarySearch(da->data, da->size, testValue); if (index < da->size && EQ(da->data[index], testValue)) return 1; return 0; }

10 Add Question: What is the complexity? O( ?? ) int sortedArrayAdd
(struct dyArray *da, EleType newValue) { int index = binarySearch(da->data, da->size, testValue); dyArrayAddAt(da, index, newValue); } Question: What is the complexity? O( ?? )

11 Remove – O( ?? ) int sortedArrayRemove (struct dyArray *da, EleType testValue) { int index = binarySearch(da->data, da->size, testValue); if (index < da->size && EQ(da->data[index], testValue)) dyArrayRemoveAt(da, index); }

12 Programming Assignment 4
You will get to finish the implementation of sortedArray operations in programming assignment 4. Each of the operations is about two lines long. Get to explore the difference between O(log n) search and O(n) search.

13 Is sorting the only reason for keeping a collection in order?
But, there is another reason for keeping elements of a collection in order. Fast merge, union and intersection

14 Fast Merge

15 Set operations are merge-like
You can quickly merge two ordered collections (array or linked list) into a NEW ordered collection. Intersection, union, different, subset are similar to merge

16 Example, Intersection Let I, j be index into two collections d, e
While I < size of d and j < size of e If d[I] < e[j] advance I Else if d[I] > e[j] advance j Else they are equal, so add to intersection and advance both

17 Example, Union (unique elements)
Let I, j be index into collections d, e While I < size of d and j < size of e If d[I] < e[j] then add d[I] to union, I++ Else if d[I] > e[j] then add e[j] to union, j++ Else add d[I] to union, I++, j++ After loop, add rest of d and rest of e to union

18 Difference (D - E) Let I, j be index into collections d, e
While I < size of d and j < size of e If d[I] < e[j] then add d[I] to diff, I++ Else if d[I] > e[j] then j++ Else I++, j++

19 NOW it’s your turn Again, I’m not going to make you do these, although they are each about lines of code. Any questions? If not, that’s all for today.


Download ppt "Sorted Dynamic Array Bag and Set"

Similar presentations


Ads by Google