240 likes | 345 Views
Linked Lists. CSC 172 SPRING 2002 LECTURE 3. Agenda. Average case lookup – unsorted list & array Amortized insert for array Sorted insert Binary search. Workshop sign-up. Still time : Dave Feil-Seifer Ross Carmara. Average Case Analysis.
E N D
Linked Lists CSC 172 SPRING 2002 LECTURE 3
Agenda • Average case lookup – unsorted list & array • Amortized insert for array • Sorted insert • Binary search
Workshop sign-up Still time : Dave Feil-Seifer Ross Carmara
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?
Lookup: Array public boolean lookup(Object o){ for (int j = 0 ; j < length;j++) if (datum[j].equals(o)) return true; return false; }
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; }
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?
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
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)*
In order to calculate expected work • Prove • Then, Expected work == (1/n)*(n(n+1)/2)) == (n+1)/2 Or about n/2 So, proof is important: Thus, chapter 2
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
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
Cost of inserting 16 items – array[2] So, what is the average (amortized) run time? We will formalize this proof Later in the course.
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?
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?)
So, suppose we kept the list sorted • Each insert now costs “n” • But what happens to lookup?
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 You know with “higher” or “lower” you can divide The remaining search space in half (i.e. log2(64)==6)
Binary Search • Recursively divide the array half until you find the item (if it is sorted).
Lookup on sorted array public boolean lookup(Object o) { return lookupRange(Object o,0,length-1); }
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] } }
So, • For the price of “n” instead of “1” on insertion We can go from “n/2” to log2(n) on lookup. • Which is “better”? • Constant insert & n lookup • n insert & log2(n) lookup
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.