Chapter6 Analysis
Chapter6 Analysis
Ordering:
1 log 2 N N N log 2 N N N 2 3
2 3 N N
Order of Magnitude Analysis (cont)
Furthermore, simply ignore any constants in front of
term and simply report general class of the term:
31N 3 4 3N 15N log 2 N
50 N 2 31N 3 24 N 15 grows proportionally to N 3
3N 2 N 21 4 3N grows proportionally to 3 N
When comparing algorithms, determine formulas to
count operation(s) of interest, then compare
dominant terms of formulas
Big O Notation
Algorithm A requires time proportional to f(N) -
algorithm is said to be of order f(N) or O(f(N))
Definition: an algorithm is said to take time
proportional to O(f(N)) if there is some constant C
such that for all but a finite number of values of N,
the time taken by the algorithm is less than C*f(N)
Examples:
50 N 2 31N 3 24 N 15 is O( N 3 )
3N 2 N 21 4 3N is O(3N )
If an algorithm is O(f(N)), f(N) is said to be the
growth-rate function of the algorithm
Example: Searching Sorted Array
Algorithm 1: Sequential Search
int search(int A[], int N, int Num) {
int index = 0;
1 3 6 7 2 4 5 8
1 2 3 4 5 6 7 8
How to Merge
• Copy the two segments into two other arrays
• Copy the smaller of the two elements from the
front of the two arrays back to the original array
original array
1 3 6 7 2 4 5 8
new array 1
1 3 6 7
new array 2
1 3 2 4 5 8
2
1 2 3 4 5 6 7 8
original array
Merging Two Array Segments
void merge(int A[], int s1low, int s1high,
int s2low, int s2high) {
int n1 = s1high - s1low + 1;
int n2 = s2high - s2low + 1;
int temp1[ASIZE];
int temp2[ASIZE];
int dst, src1, src2;
Copies O( N log 2 N )