CSC153, Class 36: Arrays
Overview:
* About data structures
* About arrays
* Arrays in Java
* A Problem: Fibonacci Numbers
* A Problem: Box Packing
Notes:
* About extra credit
* Admitted students may be visiting
* Q on HW3?
----------------------------------------
Computer Science is the study of Algorithms
and Data Structures
Algorithms: Instructions for processing information
Data Structure: Mechanism for organizing information
The array is one of the core data structures
* Key aspect: Indexed collections of information
With most users expecting O(1) lookup and set
* Typically fixed-size collections of information
Java's Perspective
* Arrays are *homogeneous*
+ All the elements have the same type
+ In contrast to Scheme in which vectors are heterogeneous
* Arrays are *fixed-size*
+ In contrast to some languages, which provide "dynamic arrays"
* Arrays are called arrays
+ In contrast to some languages which have "Vectors" or
other things that look a whole lot like arrays
+ Java has a variant of arrays called vectors
* Arrays are indexed from 0 to size-1
+ Some languages let you index from n to m where you choose
the m and the n.
* You can find out the size of an array
+ In C, the programmer is responsible for keeping track
of the size of the array.
Some details:
Declaration:
type name[size];
Student cs153[7];
Putting elements into arrays
name[location] = value
cs153[0] =
new Student("Case", "Evan", "caseevan@grinnell.edu");
Getting elements from arrays
name[location]
out.println(cs153[6].getName());
These array operations throw a RuntimeException named
IndexOutOfBoundsException
Getting the size of arrays
name.length
Declaration, revisited
type name[] = { value1, value2, ..., valuen };
Student cs153[] = {
new Student("Case", "Evan"),
new Student("Kazinka", "Shobha"),
new Student("Nnadi", "Oge")
};
Using Arrays
Fibonacci sequence
0 1 1 2 3 5 8 13 21 34 ...
0th fib number is 0
1st fib number is 1
nth fib number is n-1th fib number + n-2nd fib number
public static int fib(int n) {
if (n < 2)
return n;
else
return fib(n-1) + fib(n-2);
}
Whaddaya think?
* Short
* Schemeish
* Closely matches definition
* A lot lot lot of function calls
t(0) = 1
t(1) = 1
t(n) = t(n-1) + t(n-2) >= 2*t(n-2)
t(n) >= 2*t(n-2)
>= 4*t(n-4)
>= 8*t(n-6)
>= 16 * t(n-8)
2^k * t(n-2*k)
When k = n/2
2^(n/2) * t(n-2*n/2)
= 2^(n/2) * 1
The problem: You recompute values again and again and again ...
Once you've computed fib(k), you shouldn't have to compute
it again.
Cool idea: Build an array that stores the already-computed
values.
/* Pseudocode */
public static int fib(int n) {
if we've already computed fib(n)
return the previously computed value
else if (n < 2)
return n;
else
return fib(n-1) + fib(n-2);
}
/* Assume we've built an array to hold values, but haven't
filled it in yet. */
int Fib[SOME_REALLY_BIG_INTEGER] = { -1, -1, ..., -1 };
public static int fib(int n) {
if (Fib[n] != -1)
return Fib[n];
else if (n < 2) {
Fib[n] = n;
return Fib[n];
}
else {
Fib[n] = fib(n-1) + fib(n-2);
return Fib[n];
}
}
Hey, where does this array really exist?
public class FibonacciComputer {
int Fib[];
public FibonacciComputer(int MAX) {
Fib = new int[MAX];
for (int i = 0; i < MAX; i++) {
Fib[i] = -1;
} // for
Fib[0] = 0;
Fib[1] = 1;
}
Alternative: Build the array from left to right
----------------------------------------
New problem:
Each friend has boxes of varying sizes.
They will pay you varying amounts for the varying boxes.
Goal: Make the most money given a fixed amount of space
* Simplifying assumptions
+ If the sum of the volumes of the boxes <= volume
available in Shobha's basement, they'll all fit
+ Shobha has so many friends that we can assume that
for each chosen size/value pair, we have an arbitrarily
large of boxes of that size and value.
Strategy one (Greedy)
* Find the box that has the greatest value/size ration
* Take as many as fit
* Repeat for any remaining space
Crazy case in which is this incorrect
Shobha has 100 cubic feet
Brian has a box that is 80 cubic feet and he'll pay 90cents
Arjun has a box that is 50 cubic feet and he'll pay 50 cents
Oge has a box that is 50 cubic feet and he'll pay 50 cents