CS62 - Fall 2020 - Class 7

Example code in this lecture

   BinarySearchExamples
   ArrayList

Lecture notes

  • admin
       - Darwin part 1 (World and Species) due tonight
       
  • look at search1 method in BinarySearchExamples code
       - what does this method do?
       - what is the running time, using Big-O notation?
          - depends! sometimes an method/approach always has the same running time
             - look at search1a method in BinarySearchExamples code
          - In general, we'll talk about three different things for a method:
             - best case running time
             - worst case running time
             - average case running time
       - what are the best, worst and average case running times
          - best: O(1), constant, when the example is the first element
          - worse: O(n), linear, when the example is the last element
          - average: O(n), linear, on average, we'll need to traverse half of the elements (~n/2) which is still O(n)

  • look at search2 method in BinarySearchExamples code
       - what does this method do?
          - uses a helper method
          - does the same thing as the previous, but using recursion
       - which version is better?
       - what is the running time, using Big-O notation?
          - same as for the iterative version

  • can we do better than either of these procedures?
       - without any preprocessing of the data, no

  • Last time we played the number guessing game. How were you able to find the number without just listing a bunch of random numbers?
       - When we guessed a number, we were told larger or smaller which allowed us to eliminate a bunch of numbers   

  • what if I told you the data was in sorted order?
       - how do you find information in a phonebook (where the data is in essence, sorted)?

  • look at search2 method in BinarySearchExamples code
       - what does this method do?
          - uses a helper method
          - does the same thing as the previous, but using recursion
       - which version is better?
       - what is the running time, using Big-O notation?
          - same as for the iterative version

  • can we do better than either of these procedures?
       - without any preprocessing of the data, no

  • Last time we played the number guessing game. How were you able to find the number without just listing a bunch of random numbers?
       - When we guessed a number, we were told larger or smaller which allowed us to eliminate a bunch of numbers   

  • what if I told you the data was in sorted order?
       - how do you find information in a phonebook (where the data is in essence, sorted)?

  • show binarySearch method in BinarySearchExamples code
       - what does the code do?
          - keeps a low and a high value
          - we know that if findMe is in the array, then nums[low] <= findMe <= nums[high]
          - picks the middle element between low and high
          - compares that middle element to our value and then either finds the data or DISCARDS HALF OF THE REMAINING DATA
       - an example
          - 1, 3, 7, 15, 16, 18, 21, 40, 45, 50
       - what's the running-time?
          - best case: O(1) it's the midpoint in the array
          - worst case: not found
             - how many times do we iterate through the while loop?
             - let's consider the case where the number of elements is a power of 2
                - we could always pad it up do the next largest power of 2
             - at each iteration we throw away half of the data, n/2, n/4, n/8
             - when will it be done?
                - when n/2^i = 1
                log n/2^i = log 1
                log n - log 2^i = 0
                log n - 2 log i = 0
                log n = 2 log i
                i = log_2 n
             - runtime is O(log_2 n)
          - average case: half as many iterations through the while loop... still O(log_2 n)

  • how would we write a recursive version?
       - we'd need a helper method
       - rather than keeping low and high as variables, they'll be parameters
       - then, rather than adjust them, we just call recursively call our method with smaller values

  • show binarySearchRecursive in BinarySearchExamples code

  • Last comments on binary search
       - It‚ is easy to get indices wrong, so be careful!
          - "Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky... Professor Donald Knuth (taken from http://en.wikipedia.org/wiki/Binary_search_algorithm)
       - http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
       - How hard is it? http://portal.acm.org/citation.cfm?doid=358476.358484, only accessible on campus
       - What if we wanted to return the first one in the list?

  • Lists
       - lists are general structures that store sequential data
       - the key operations are that we can get and set values at indices, but other operations will be useful.
       - the BasicList interface in ArrayList code defines a very basic interface for lists
          - get/set
          - add (add to the end)
          - size

  • ArrayLists
       - Java has a List interface for data structures that store data sequentially:
          - https://docs.oracle.com/javase/8/docs/api/java/util/List.html
          - lots of methods, but key: get and set and particular indices

       - The ArrayList class implements this interface
       
       - Why do you think its called "ArrayList"?
          - the underlying structure that it stores the data in is an array

  • look at the BasicArraylist class in ArrayList code
       - keep track of two instance variables
          - data (the actual data)
          - size (the number of elements in the data)
             - note this is different than the size of the data array
       
       - Anything new/unusual in the constructor?
          - we cannot create new arrays of type E (or of any type variable)
             - this has to do with how Java implemented generics
          - instead, we create a new array of Objects and type cast it to an E[]

       - look at get and set
          - what do you think the rangeCheck method does?
             - checks to see that the index is within a valid range

       - look at rangeCheck
          - if we want to have a method raise an exception, we use the keyword "throw"
             - remember, exceptions are just classes so we need to create a new instance

  • what are the run times of get, set, and size?
       - Assuming array index operations are O(1), then O(1)

  • adding to an ArrayList
       - so far, we haven't added any functionality over arrays
       
       - what was the main functionality that ArrayLists added over arrays?
          - add/extend

       - how do you think this is done?
          - we allocate some initial array
          - we fill it in as we go
          - what happens when it fills up?
             - we have to create a bigger array!
             - copy over the old values

  • what does the resize method do in the BasicArrayList class in ArrayList code ?
          - creates a new array of type E
          - copies the existing value
          - then sets data to be that new array!

       - look at add method
          - check to see if we're out of space
             - if so, resize
             - we can choose any size that's larger than the existing
             - we'll talk more about how different sizes might affect things