Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists CSC 172 SPRING 2002 LECTURE 3 Agenda  Average case lookup – unsorted list & array  Amortized insert for array  Sorted insert  Binary.

Similar presentations


Presentation on theme: "Linked Lists CSC 172 SPRING 2002 LECTURE 3 Agenda  Average case lookup – unsorted list & array  Amortized insert for array  Sorted insert  Binary."— Presentation transcript:

1

2 Linked Lists CSC 172 SPRING 2002 LECTURE 3

3 Agenda  Average case lookup – unsorted list & array  Amortized insert for array  Sorted insert  Binary search

4 Workshop sign-up Still time : Dave Feil-Seifer Ross Carmara

5 Average Case Analysis  Consider “lookup” on either a linked list or array  We said “n” run time  Under what conditions do we really get “n”  What do we get when the element is the first in list?  Second.. ?  Third.. ?  Middle.. ?  Last?

6 Lookup: Array public boolean lookup(Object o){ for (int j = 0 ; j < length;j++) if (datum[j].equals(o)) return true; return false; }

7 Lookup: Linked List public boolean lookup(Object o){ Object temp = head; while (temp != null){ if ((temp.data).equals(o)) return true; temp = temp.next; } return false; }

8 Average case  On a list of length n in how many different places can the item be?  If the list is unorganized (unsorted/random) what is the chance that the item is at any one location?  What is the probability of getting “heads” on a coin toss?  What is the probability of getting “3” rolling a die?

9 Average case analysis  For all the possible locations, multiply the chance of dealing with that location, times the amount of work we have to do at it. First location work = (1/n) * 1 Second location work = (1/n) * 2 Third location work = (1/n) * 3 …. But we have to add them all up

10 Average case analysis Expected work = (1/n)*1 + (1/n)*2+... + (1/n)*n Expected work = (1/n)*(1+2+…+n) Expected work = (1/n)*

11  Prove  Then, Expected work == (1/n)*(n(n+1)/2)) == (n+1)/2 Or about n/2 So, proof is important: Thus, chapter 2 In order to calculate expected work

12 Amortized Analysis Amortize? to deaden? Latin : ad-, to + mortus dead Modern usage: payment by installment - sort of like a student loan, where you... never mind The idea is to spread payments out

13 Amortized analysis  Spread the “cost” out over all the “events”  Consider insert() on an array  We know the worst case is when we have to expand()  Cost of “n”  But, we don’t have to do this every time, right

14 Cost of inserting 16 items – array[2] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2 2 4 4 8 8 8 8 capacity

15 Cost of inserting 16 items – array[2] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

16 Cost of inserting 16 items – array[2] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 So, what is the average (amortized) run time? We will formalize this proof Later in the course.

17 Recap  Insert on a linked list?  Constant - 1  Insert on an array?  Worst case n  Average case constant – 2  Lookup on a list?  n/2  Lookup on an array  n/2  Can we do better?

18 What if we kept the array sorted?  How do we do it?  How much does it cost?  Imagine inserting “5” into  We have to “shift” (could you write this?) 2468 24568

19 So, suppose we kept the list sorted  Each insert now costs “n”  But what happens to lookup?

20 Guessing Game  I’m thinking of a number 1<=x<=64  I’ll tell you “higher” or “lower”  How many guesses do you need?  29  47 33 You know with “higher” or “lower” you can divide The remaining search space in half (i.e. log 2 (64)==6)

21 Binary Search  Recursively divide the array half until you find the item (if it is sorted).

22 Lookup on sorted array public boolean lookup(Object o) { return lookupRange(Object o,0,length-1); }

23 LookupRange: Binary Search public static void lookupRange(Object o, int lower,int higher){ if (lower > higher) return false; else { int mid = (low + high)/2; if (datum[mid].compareTo(o) < 0) return lookupRange(o,lower,mid); else if (dataum[mid].compareTo(o) > 0) return lookupRange(o,mid+1,higher); else return true; // o == datum[mid] }

24 So,  For the price of “n” instead of “1” on insertion We can go from “n/2” to log 2 (n) on lookup.  Which is “better”?  Constant insert & n lookup  n insert & log 2 (n) lookup

25 All very well and good  But can it help me get a date?  YES!  In order to get a date you have to look up someone’s phone number in the phone book.  You can save time by using binary search.


Download ppt "Linked Lists CSC 172 SPRING 2002 LECTURE 3 Agenda  Average case lookup – unsorted list & array  Amortized insert for array  Sorted insert  Binary."

Similar presentations


Ads by Google