Commit ff19fef8 authored by michael lundquist's avatar michael lundquist
Browse files

Finishing ch 3, starting ch 4.

parent 6aa6d68b
......@@ -262,15 +262,18 @@ public class Rando{
### 3.6.1 Cloning Arrays
- As discussed above, assignment just copies a reference to a variable, clone does a shallow copy.
- Here's some code to deep clone a 1d array:
- To clone an array or multi-dimensional array you have to iterate over the elements individually. This will require some thinking.
public Object[] deepClone(Object o){
Person[] guests = new Person[contacts.length];
for(int k=0; k < contacts.length; k++)
guests[k] = (Person) contacts[k].clone();
### 3.6.2 Cloning Linked Lists
- Classes that can be cloned should implement the Cloneable interface.
public class SinglyLinkedList<E> implements Cloneable{
\ No newline at end of file
- When cloning a class, you generally call `super.clone()` on the first line to get a new Object, then that object is modified to suit your needs.
- In a linked list, modifying the new object means:
- keeping the size
- cloning each of the nodes and re-linking them iteratively.
\ No newline at end of file
......@@ -6,33 +6,10 @@ import java.util.Random;
public class Examples{
public static void main(String[] args){
//CustomS_B[] vars = {new Examples.CustomS_B("abc"),new Examples.CustomS_B("DEF"),new Examples.CustomS_B("ghi")};
Integer[] vars = {1,2,3,4};
* A way of cloning an array of clonables.
* Works with multi dimensional arrays
public static Integer[] deepClone(Integer[] o){
//Class oType = o.getClass();
//Cloneable[] newClone = o.clone();//Array.newInstance(oType, o.length);
Integer[] newClone = o.clone();
for(int k=0; k < o.length; k++){
newClone[k] = deepClone((Integer[]) o[k]);
Cloneable newObj = o[k];
newClone[k] = newObj.clone();
return newClone;
# ch 4 Algorithm Analysis
- __Data structure__: a systematic way of organizing and accessing data.
- __Algorithm__: a step-by-step procedure for performing some task in a finite amount of time.
- Algorithms and data structures are measured by:
- Runtime (aka time complexity), or the number of steps they take.
- Space complexity, the number of things they have to store.
- Note, different algorithms and data structures will work differently on different environments (hardware environments, OSs...).
- We focus on how time and space complexity grow as the size of the input grows.
## 4.1 Experimental Studies
- The simplest way to a assess an algorithm is timing it by comparing the time before and after the algorithm.
- To get the time in milliseconds since the epoch (1/1/1970), run `System.currentTimeMillis();`
- To get time in nanoseconds rather than milliseconds, use `System.nanoTime()`.
- Not sure when this rolls back to 0 though...
- Run your algorithm on multiple input sizes and plot how long each input took vs the input size.
- This method is effective as long as the various tests are performed in the same environment, but results will vary in different environments.
- They show how much faster java's StringBuilder is than String for certain things.
#### Challenges of Experimental Analysis
- Runtimes are machine dependent
- You can't test every input in your experiment
- Your algorithm must be fully implemented before you can test it.
- This could be a disaster.
### 4.1.1 Moving Beyond Experimental Analysis
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment