Linked Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Linked Lists

Description:

But what happens to lookup? Guessing Game. I'm thinking of a number 1 =x =64 ... order to get a date you have to look up someone's phone number in the phone book. ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 25
Provided by: thaddeusf
Category:
Tags: linked | lists | lookup | number | phone

less

Transcript and Presenter's Notes

Title: Linked Lists


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 lt lengthj)
  • if (datumj.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)(12n)
  • Expected work (1/n)

10
In order to calculate expected work
  • Prove
  • Then,
  • Expected work (1/n)(n(n1)/2)) (n1)/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 dont have to do this every time, right

13
Cost of inserting 16 items array2
capacity
14
Cost of inserting 16 items array2
15
Cost of inserting 16 items array2
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
  • Im thinking of a number 1ltxlt64
  • Ill 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 gt higher) return false
  • else
  • int mid (low high)/2
  • if (datummid.compareTo(o) lt 0)
  • return lookupRange(o,lower,mid)
  • else if (dataummid.compareTo(o) gt 0)
  • return lookupRange(o,mid1,higher)
  • else return true // o datummid

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
    someones phone number in the phone book.
  • You can save time by using binary search.
Write a Comment
User Comments (0)
About PowerShow.com