1 / 24

Linked Lists

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.

les
Download Presentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists CSC 172 SPRING 2002 LECTURE 3

  2. Agenda • Average case lookup – unsorted list & array • Amortized insert for array • Sorted insert • Binary search

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

  4. 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?

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

  6. 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; }

  7. 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?

  8. 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

  9. 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)*

  10. 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

  11. 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

  12. 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

  13. Cost of inserting 16 items – array[2] capacity

  14. Cost of inserting 16 items – array[2]

  15. Cost of inserting 16 items – array[2] So, what is the average (amortized) run time? We will formalize this proof Later in the course.

  16. 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?

  17. 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?)

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

  19. 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)

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

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

  22. 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] } }

  23. 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

  24. 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.

More Related