CS 46B - Lecture 10
Pre-class reading
Two Golden Rules for Pseudocode

- Each pseudocode statement corresponds to one or a small number of Java statements
- Good: ch = first character of str
- Good: Set all elements of arr to 0
- Bad: Break the input into strings in the other set and not in the other set
- How do you do that?
- The point of pseudocode is settling such a question before coding
- Each pseudocode statement can be unambigously translated to Java
- Good: Add c0 to the end of p
- Good: k = n / 2 if n is even or (n + 1) / 2 if n is odd
- Bad: Add the first half of the points to p
- What is “the first half” if the number of points is odd?
- The point of pseudocode is settling such a question before coding
Selection Sort
- Find the smallest and swap it with the first element
- Find the next smallest. It is already in the correct place
- Find the next smallest and swap it with first element of unsorted portion
- Repeat
- When the unsorted portion is of length 1, we are done
Selection Sort on Various Size Arrays*
n |
Seconds |
10,000 |
0.786 |
20,000 |
2.148 |
30,000 |
4.796 |
40,000 |
9.192 |
50,000 |
13.321 |
60,000 |
19.299 |
- Doubling the size of the array more than doubles the time needed to sort it
- Look closely: 10,000 -> 20,000 -> 40,000 or 30,000 -> 60,000
- Doubling the size makes the running time go up about four times
* Obtained with a Pentium processor, 2 GHz, Java 6, Linux
Selection Sort on Various Size Arrays
Lecture 10 Clicker Question 1
On that particular machine, how long would it take to sort an array of 100,000 elements?
- About 8 seconds
- About 26 seconds
- About 53 seconds
- About 100 seconds
Analyzing the Performance of the Selection Sort Algorithm
- In an array of size n, count how many times an array element is visited
- To find the smallest, visit n elements + 2 visits for the swap
- To find the next smallest, visit (n - 1) elements + 2 visits for the swap
- The last term is 2 elements visited to find the smallest + 2 visits for the swap
Analyzing the Performance of the Selection Sort Algorithm
- The number of visits:
- T(n) = n + 2 + (n - 1) + 2 + (n - 2) + 2 + . . .+ 2 + 2
- This can be simplified to n2 /2 + 5n/2 - 3
- 5n/2 - 3 is small compared to n2 /2 – so let's ignore it
- Also ignore the 1/2 – it cancels out when comparing ratios
Analyzing the Performance of the Selection Sort Algorithm
- The number of visits is of the order n2
- Using big-Oh notation: The number of visits is O(n2)
- To convert to big-Oh notation: locate fastest-growing term, and ignore constant coefficient
- n2 /2 + 5n/2 - 3 -> O(n2)
- Multiplying the number of elements in an array by 2 multiplies the processing time by 4
- T(2n) / T(n) = (2n)2/n2 = 4n2/n2 = 4
Lecture 10 Clicker Question 2
How long would it take to sort an array of 1,000,000 elements?
- About 10 times as long as sorting an array of 10,000 elements
- About 100 times as long as sorting an array of 10,000 elements
- About 1000 times as long as sorting an array of 10,000 elements
- About 10,000 times as long as sorting an array of 10,000 elements
Lecture 10 Clicker Question 3
Here is the Albanian sort algorithm:
Randomly shuffle the array
Check if it is sorted
If not, repeat
What is the big-oh complexity?
- O(n2)
- O(2n)
- O(nn)
- None of the above
Big-Oh Isn't Just for Sorting
- Finding largest number in an array
- Set largestSoFar to a[0]: 1 visit
- Compare largestSoFar with a[1]: 1 visit
- Compare largestSoFar with a[2]: 1 visit
- ...
- Compare largestSoFar with a[n - 1]: 1 visit
- Total: n visits
- Finding largest number is O(n)
Finding the Most Frequent Element
-
- Ok, that was easy: 7 is the most frequent
- But what if there are thousands of values?
-
- Need an algorithm
- Count how often 8 occurs
- Count how often 7 occurs
- Repeat for each element
Big-Oh of Most Frequent Element
- Each count is O(n) (Why?)
- There are n elements
- All counts: O(n2)
- Now need to find largest count
- That's O(n)
- All together: O(n2) + O(n) = O(n2)
Lecture 10 Clicker Question 4
Maybe it's better to sort the array first? Then identical elements are next to each other, and you can find the most frequent one in a single pass through the sorted array. What is the big-Oh efficiency of that, assuming that we use selection sort for the sorting?
- O(n)
- O(n2 + n)
- O(n2)
- O(n3)