Exploring Sorting
Learning Target & Do Now
LT: By the end of class, I will have my own conception of how to use computers to sort information.
DN: Refer to the provided handout for today.
Programming a Sorting Algorithm
Homework
Today’s animal: Giraffe Weevil
Selection Sort
Bubble Sort
Learning Target & Do Now
LT: By the end of today, I will know Selection and Bubble sorts.
DN: What is the complexity (order of growth) for the following?
public static void foo(int[] A){
for(int i=0; i<A.length; i++)
for(int j=0; j<A.length; j++)
System.out.println(“*”);
}
Answer: n2, or quadratic
Selection Sort
Let’s sort the array [4, 6, 1, 8, 7, 5, 9, 10]
[4, 6, 1, 8, 7, 5, 9, 10]
Pseudocode
public static void sort(int[] A){
for(int i=0; i<A.length; i++){
// find the smallest value beginning at index i
// switch the elements at index i and the index of the min value
}
}
Time Complexity of Selection Sort
Summation Made Simple
n + (n-1) + (n-2) + … + 2 + 1
Is the same as
Is the same as
Identify the dominating term!
Bubble Sort
Pros: partially sorts remaining items in each loop, can complete early on partially sorted data
Cons: variable action count, possibility for higher action count than other n2 sorts
One Iteration
One Iteration, Second Element
for(int i = 0; i < A.length-1; i++){
if(A[i] > A[i + 1]){
swap(A, i, i + 1);
}
}
for(int i = 0; i < A.length-2; i++){
if(A[i] > A[i + 1]){
swap(A, i, i + 1);
}
}
What changed?
Bubble Sort
public static void bubbleSort(int[] A){
for(int i = 1; i < A.length; i++){
for(int j = 0; j < A.length-i; j++){
if(A[j] > A[j+1]){
swap(A, j, j+1);
}
}
}
}
Homework
Today’s animal: Portuguese man o’ war
Modified Bubble Sort;
Insertion Sort
Do Now
LT: By the end of class, I will have a more refined sense of how sorting algorithms work.
DN: If bubble sort makes no swaps during an iteration, what does that tell you about the input array?
A: The array is already sorted.
Bubble Sort, cont.
Case | Conditions | Complexity |
Best | | |
Worst | | |
Average | N/A | |
Insertion Sort
Insertion Sort!
Insertion Sort Code
public static void insertionSort(int A[]){
int j;
for(int i = 1; i < A.length; i++){
j = i;
while(j > 0 && A[j] < A[j-1]){
swap(A, j, j-1);
j--;
}
}
}
Analyzing Insertion Sort
Pros:
Cons:
Complexity of Insertion Sort
Case | Conditions | Complexity |
Best | | |
Worst | | |
Average | N/A | |
Homework
Today’s animal: King� Cheetah (has stripes� instead of spots)
Introduction to Recursion
Learning Target & Do Now
LT: By the end of class, I will have an idea of what recursion is.
DN: What are the next six terms in the sequence:
1, 1, 2, 3, 5, 8, 13, 21, 34, …
What is the name of the sequence?
Answer: 55, 89, 144, 233, 377, 610; Fibonacci Sequence
Arithmetic and Geometric Sequences
Fibonacci is a great example of a recursive function that is easier than the explicit
Pros and Cons
The Fibonacci Sequence
The explicit Fibonacci sequence, a.k.a Binet’s Formula:
The recursive sequence: Fn = Fn-1 + Fn-2
Programming Geometric Sequences
Explicit:
public static double geometric(double first_term, double r, int term){
return first_term * Math.pow(r, term-1);
}
Recursive (Note the base case!):
public static double geometric(double first_term, double r, int term){
if(term == 0)
return first_term;
return r * geometric(first_term, r, term-1);
}
Programming Factorial
5! = 5*4*3*2*1
4! = 4*3*2*1
5! == 5 * 4!
Coding Fibonacci
Homework
Today’s animal: Victoria Crowned Pigeon
Test Day
Learning Target & Do Now
LT: Today I will demonstrate my mastery of concepts relating to searching, sorting, and complexity.
DN: Complete any last-minute preparation.
Homework
Today’s animal: Humpback whales