You are on page 1of 27

CHAPTER 11

ANALYSIS OF ALGORITHMS
PROBLEM TO SOLVE

My program finds all prime numbers between 2 and 1, 000, 000, 000 in
1.37 seconds
Is this
A. Good
B. Bad
C. It Depends
ALGORITHM ANALYSIS

Algorithm Analysis the time it takes to run code


ANALYZING CODE

Consider the size of the problem


Washing Dishes is the number of dishes
Sorting an array is the number of elements in the array

Evaluate what I am trying to optimize


Washing Dishes is the number of times a dish is dried
Sorting an array is the number of comparisons between
elements
ANALYSIS OF ALGORITHMS

Algorithm Efficiency: consider the size of the problem and the


processing steps
Growth Function: Time complexity that shows the relationship
between
size of the problem relative to n
Only DOMINANT TERM
processing steps
Want to optimize
GROWTH FUNCTION
BIG O

Asymptotic Complexity
Order of Magnitude of the algorithm
Called Big O
Worst Case time to solve problem
Longest possible execution time
More helpful than BEST CASE
Know it will NEVER take more time than this to solve problem
GROWTH FUNCTION
GROWTH FUNCTION: WASHING DISHES

Example 1:
O(n)
T(n) = 30n + 30n

Example 2:
O(n2)
T(n) = 15n2 + 45n
GROWTH FUNCTION: SORTING AN ARRAY

What about sorting an array?


10 9 8 7 2 5 4 1

v.

1 2 4 5 7 8 9 10

Lets look at some examples & code first before answering this question!
BIG O EXAMPLES

(2 + 1)2

A. O(n)
B. O(n2)
C. O(n3)
D. O(2n)
BIG O EXAMPLES

(2 + 2)2

A. O(n)
B. O(n2)
C. O(n3)
D. O(2n)
BIG O EXAMPLES

n3 + 100n2 + n
A. O(n)
B. O(n2)
C. O(n3)
D. O(2n)
BIG O EXAMPLES

2n + 100n2 + 45n
A. O(n)
B. O(n2)
C. O(n3)
D. O(2n)
BIG O EXAMPLES

n2n+n22n
A. O(n)
B. O(n2)
C. O(2n)
D. O(n22n)
COMPARISON OF GROWTH FUNCTIONS
BIG O OF LOOPS

for (int count = 0; count < n; count++) { n times, O(n)


// O(1) steps
}

for (int count = 0; count < n; count+=2) { n/2 times, O(n)


// O(1) steps
}
BIG O OF LOOPS

count = i;
while (count < n) { Increases by powers of 2,
count *= 2; O(log2n)
// O(1) steps
}
Multiply complexities:
for (int count = 0; count < n; count+=2) {
O(n) * O(n)
for(int count2 = 0; count2 < n; count2++) {
O(n2)
// O(1) steps
}
}
SELECTION SORT

public static void selection(int [ ] array) {


int startScan, index, minIndex, minValue;
for (startScan = 0; startScan < (array.length-1); startScan++) {
minIndex = startScan;
minValue = array[StartScan];
for(index=(startScan+1); index < array.length; index++) {
if (array[index] < minValue) {
minValue = array[index];
minIndex = index;
}
}/inner for-loop
array[minIndex] = array[startScan];
array[startScan] = minValue;
}//outer for-loop
}//end selectionSort
SELECTION SORT

public static void selection(int [ ] array) {


int startScan, index, minIndex, minValue;
for (startScan = 0; startScan < (array.length-1); startScan++) {
minIndex = startScan;
O(n2) minValue = array[StartScan];
for(index=(startScan+1); index < array.length; index++) {
if (array[index] < minValue) {
minValue = array[index];
minIndex = index;
}
}/inner for-loop
array[minIndex] = array[startScan];
array[startScan] = minValue;
}//outer for-loop
}//end selectionSort
BIG O OF SORTING & SEARCHING ALGORITHMS

Algorithms Time Complexity


Sort Best Case Average Case Worst Case
Selection Sort O(n2) O(n2) O(n2)
Bubble Sort O(n) O(n2) O(n2)
Insertion Sort O(n) O(n2) O(n2)
Mergesort O( n*log(n) ) O( n*log(n) ) O( n*log(n) )
Quick Sort O( n*log(n) ) O( n*log(n) ) O(n2)
Search
Sequential O(n)
Binary O( log(n) )
BIG O OF METHODS

for (int count = 0; count < n; count++) Case 1


{ public void printSum(int count) {
printSum(count);
int sum = 0;
}
for(int i = 0; i < count; i++)
sum += i;
O(n) * O(n) = O(n2) System.out.println(sum);
}
BIG O OF METHODS

for (int count = 0; count < n; count++) Case 2


{ public void printSum(int count) {
printSum(count);
int sum = count * (count-1)/2;
}
System.out.println(sum);
}
O(n) * O(1) = O(n)
BIG O EXAMPLES

for (int i = 0; i < n; i++) {


for int j = 0; j < n; j++) {
// O(1) steps
}
}
A. O(1)
B. O(n)
C. O(n2)
D. O(n3)
BIG O EXAMPLES

for (int i = 0; i < n; i++) {


for int j = 0; j < i; j++) {
// O(1) steps
}
}
A. O(1)
B. O(n)
C. O(n2)
D. O(n3)
BIG O EXAMPLES

for (int i = 0; i < n; i++) {


for int j = 0; j < 20; j++) {
// O(1) steps
}
}
A. O(1)
B. O(n)
C. O(n2)
D. O(n3)
BIG O EXAMPLES

for (int i = 0; i < n; i++) {


for (int j = 0; j < 20; j++) {
// O(1) steps
}
}
for (int k = 0; k < 10; k++) {
// O(1) steps
}
A. O(1)
B. O(n)
C. O(n2)
D. O(n3)

You might also like