Big O and Sorting

COMP 53 – Week Seven
Big O
Sorting
Complete Lab 5 (if needed)
Topics
O Big O Definition
O Searching Algorithms
O Sorting Algorithms
Why Do We Care
O Programs go fast!
O Faster the better
O Fastest is bestest!?
O Space-Time Continuum
O Is Time More Important than Space?
1-4
Countin’ is Important
O Recall algorithm definition:
O Set of instructions for performing a task
O Can be represented in any language
O Typically thought of in "pseudocode"
O Considered "abstraction" of code
O Abstract Data type - "table"
O Based on input size
O Table called "function" in math (with arguments and return values)
O Argument is input size: T(10), T(10,000), …
O Function T is called "running time"
Counting Operations
O T(N) given by formula, such as:
T(N) = 5N + 5
O Must be "computer-independent"
O Doesn’t matter how "fast" computers are
O Can’t count "time"
O Instead count "operations"
19-6
On inputs of size N
program runs for
5N + 5 time units
Counting Example
O Goal – print out array values until a
specific value is found
O Find() function determines where in
the array the value is. Assume max
= 100.
int find(char arr[], char target)
{
for (int i = 0; i < 100; i++)
If (arr[i] == target)
Return(i);
Return (-1);
}
void printPartial1(
char arr[], char last) {
for (int i = 0; i < find(arr,
last); i++)
cout << arr[i];
}
void printPartial2(
char arr[], char last) {
int end = find(arr, last);
for (int i = 0; i < end; i++)
cout << arr[i];
}
5 operations per
loop
2 operations
Now Let’s Do Some Math
Array Size
printPartial1
printPartial2
0
?
?
3
?
?
50
15554
656
100
61104
1306
150
136654
1956
200
242204
2606
V1 = 6n2 + 5n + 1
O(n2)
V2 = 5n+2  O(n)
Which One is Better?
Instructions Executed
250000
200000
150000
printPartial1
printPartial2
100000
50000
0
0
50
100
150
Maximum Array Size
200
Counting Operations Example 2
O int I = 0;
bool found = false;
while (( I < N) && !found)
if (a[I] == target)
found = true;
else
I++;
O 5 operations per loop iteration:
<, &&, !, [ ], ==, ++
O After N iterations, final three: <, &&, !
O So: 6N+5 operations when target not found
19-10
Some constant "c" factor where
c(6N+5) is actual running time
c different on different systems
We say code runs in time O(6N+5)
Only consider "highest term"
Term with highest exponent
 O(N) here
Big-O Terminology
O Linear running time:
O O(N)—directly proportional to
input size N
O Quadratic running time:
O O(N2)
O Logarithmic running time:
O O(log N) - "log base 2"
O Very fast sort algorithms!
O Exponential running time
O O(2N)
O Chess and gaming algorithms
19-11
Sorting is Expensive
O Faster on smaller input set?
O Perhaps
O Might depend on "state" of set
O "Mostly" sorted already?
O Consider worst-case running time
O T(N) is time taken by "hardest" list
O List that takes longest to sort
19-12
Standard Template Running Times
O O(1) - constant operation always:
O Vector inserts to front or back
O deque inserts
O list inserts
O O(N)
O Insert or delete of arbitrary element in vector
or deque (N is number of elements)
O O(log N)
O set or map finding
19-13
Topics
O Big O Definition
O Searching Algorithms
O Sorting Algorithms
Return Midterms
O Review key problems
Searching an Array
(1 of 4)
5-16
Searching an Array
(2 of 4)
5-17
Searching an Array
(3 of 4)
Data type could be
changed to
template <class T>
5-18
Searching an Array
(4 of 4)
What Oh is this
search?
5-19
Can We Go Faster! You Betcha!!!
O In pervious example, search started at beginning of array
O Is this required
O Think about divide and conquer
O What If… Array is already sorted?
O Think about a phone book search – where do you start?
O How is the data divided
O AKA – Binary Search
O Back to our old friend - Recursion
Binary Search Template
template <class T>
int binarySearch(T arr[], T item, int start, int end) {
int mid = (start + end) / 2;
if (arr[mid] == item)
return mid;
else if (start >= end)
return -1;// NOT found
if (arr[mid] < item)
return binarySearch(arr, item, mid + 1, end);
return binarySearch(arr, item, start, mid - 1);
}
Recursive call to split
the problem in two
Topics
O Big O Definition
O Searching Algorithms
O Sorting Algorithms
Sorting an Array
Selection Sort Algorithm
Find the smallest
value in the array.
Swap with first
element
Move to next element,
find smallest value in
remainder
5-23
Selection Sort
(1 of 4)
Again – int can be
replaced with Template
5-24
Selection Sort
(2 of 4)
5-25
Selection Sort
(3 of 4)
What O(h) is
sort()?
What O(h) is
Swap?
5-26
Selection Sort
(4 of 4)
What O(h) is
indexOfSmallest?
What O(h) is
total sort?
Need to trace
5-27
In Class Exercise
O Who’s the shortest?
O Who’s the tallest?
Bubble Sort Option
Animation
Other Sort Algorithms
mergeSort()
O Approach
O Break array into halves until
size = 1
O Merge sublists together so
that they are sorted
O Typically recursive solution
O O(n log n) performance
O Extra space needed due to
recursion and temporary
arrays
Other Sort Algorithms
quickSort()
O Approach
O Find the middle value in the
set such that all values to
left are smaller and to the
right are bigger
O Recursively sort each side
O Also O(n log n) performance
O O(n) extra storage space
required
Middle is
called the
pivot point
Sort Animations
Let Your Inner Geek Out
O Check out sorts in action
O https://www.youtube.com/watch?v=t8g-iYGHpEA
O Interactive Sorting Tool
O https://www.cs.usfca.edu/~galles/visualization/Compari
sonSort.html
O(h) Summary
for n = 1024
Performance Metric
O(1)
O(n)
Name
Constant
Linear
N=100
1
6.64
O(log n)
O(n log n)
O(n2)
Logarithmic
Log-linear
quadratic
100
664
10000
O(n3)
2n
N!
cubic
exponential
factorial
100000
1270000000000000000
More than a lifetime
Key Takeaways
O Big ‘O’ notation
O Indicates the relative performance of an algorithm
O Based on the size of the problem trying to solve
O Fastest search has _______________ speed
O Fastest sort has _______________ speed
O Speed vs Space
O Sometimes using more space becomes slower than wasting
execution time.
1-43