Today’s Musical Selections
"Can’t worry about what’s behind you, or what’s coming for you further up the road." At least, so says the Swedish duo First Aid Kit.
As an odd aside, the video for "Silver Lining" is the second I’ve watched recently that features a self-driving car—although, in both cases, it’s more of a metaphor than a technological statement. We’ll watch the second on Friday.
Recursion and Sorting
> Click or hit Control-Enter to run the code above
Recursive Tree Search
-
Base case: We’ve reached a node with no descendants. Return true if its value matches, zero otherwise.
-
Recursive step: Consider our right tree and left tree separately.
-
Combine results: Return true if either we or our right or left subtree contain the search value.
> Click or hit Control-Enter to run Example.main above
How Could We Make This Search More Efficient?
> Click or hit Control-Enter to run Example.main above
Recursive Tree Traversal
Let’s find all nodes in the tree and add them to a list.
What’s Our (Recursive) Algorithm?
Recursive Tree Traversal
-
Base case: We’ve reached a
null
node, at which point we can stop. -
Recursive step: Consider our right tree and left tree separately.
-
Combine results: Add ourselves to the list of nodes.
Java ArrayList
s
Lists are one of the two data structures you meet in heaven.
We’ve studied them in class together. But you’ll usually use Java`s built-in implementations.
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
List list = new ArrayList();
List anotherList = new LinkedList();
> Click or hit Control-Enter to run the code above
> Click or hit Control-Enter to run Example.main above
Other Recursive Data Structures
Every sub(blank) of a (blank) is, itself, a (blank).
-
Tree
-
(Contiguous) List
-
(Contiguous) Array
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
Just like with trees, we need a way to both make the problem smaller and identify the smallest subproblem.
-
How do we make the problem smaller? Break the list into the current item and the rest of the list.
-
What’s the smallest subproblem? A list with a single element.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
Just like with trees and lists, we need a way to both make the problem smaller and identify the smallest subproblem.
-
How do we make the problem smaller? Break the list into two smaller subarrays.
-
What’s the smallest subproblem? An array with a single item.
Questions About Recursion?
Sorting Algorithms
Sorting algorithms bring together several of the things that we have discussed recently:
-
Imperative programming
-
Big-O algorithm runtime analysis
-
Recursion
Sorting Matters
Sorting is often a building block for many other algorithms.
-
Searching is more efficient if the data is sorted first
-
Sorting can be used to detect duplicates
-
Sorting is often used to produce a canonical representation of data or for presentation to human users
In Memory of Jim Gray, Turing Award Winner
Jim Gray was a pioneer in the fields of databases and data processing.
He vanished at seat in 2007 and, despite a worldwide crowdsourced effort to locate his boat, was never found.
There Are Many Sorting Algorithms
And we won’t discuss them all…
-
Insertion sort (Friday)
-
Selection sort (lab)
-
Merge sort (lecture)
-
Quicksort (lecture)
-
Bubble sort (lab)
-
And even new ones, like Timsort (circa 2002)
Sorting Basics
-
We’ll discuss sorting on arrays which allow random access, although many algorithms will also work on lists.
-
We’ll be sorting in ascending order, although obviously descending order sorts are also possible.
-
We can sort anything that we can compare—but we’ll mostly be sorting integers.
Analyzing Sorting Algorithms
Since sorting algorithms handle data, we care about both time and space complexity.
-
Time complexity: how long does it take?
-
Space complexity: how much space is required?
CS 125 Final Project
How to Not Get a Great CS Job
All of you will get a job. Not all of you will get a great 1 job.
Here’s a good strategy for not getting a good job:
-
Take CS classes.
-
Do the projects.
-
Get good grades.
-
Don’t do any side projects: focus on grades instead.
How to Get a Great CS Job
Show your passion for technology.
-
The CS 125 Final Project is intended to get you started doing that.
Example Fall 2019 Final Projects
Announcements
-
I have virtual office hours today from 4–5PM. Please come by and say hi!