Data Structures and
Algorithm
Instructor: Javid Ali
Textbook
Mark Allen Weiss, Data Structures
and Algorithm Analysis in Java,
NELL DALE, OBJECT-ORIENTED
DATA STRUCTURES USING Java
Grading
Final exam: 50%
Midterm: 25%
Quizzes: 15%
Assignments: 10%
Java Chapter 2: Algorithm
Analysis
Application of Big-Oh to program
analysis
Running Time Calculations
Background
The work done by an algorithm, i.e.
its complexity, is determined by the
number of the basic operations
necessary to solve the problem.
The Task
Determine how the number of
operations depend on the size of
input :
N - size of input
F(N) - number of operations
Basic operations in an algorit
hm
Problem:
Find x in an array
Operation:
Comparison of x with an entry in the
array
Size of input:
The number of the elements in the array
Basic operations ….
Problem:
Multiplying two matrices with real entries
Operation:
Multiplication of two real numbers
Size of input:
The dimensions of the matrices
Basic operations ….
Problem:
Sort an array of numbers
Operation:
Comparison of two array entries plus
moving elements in the array
Size of input:
The number of elements in the array
Counting the number of ope
rations
A.
for loops O(n)
The running time of a for loop is at most
the running time of the statements
inside the loop × the number of
iterations.
for loops
sum = 0;
for( i = 0; i < n; i++ )
sum = sum + i;
The running time is O(n)
Counting the number of ope
rations
B.
Nested loops
The total running time is the running
time of the inside statements × the
product of the sizes of all the loops
Nested loops
sum = 0;
for( i = 0; i < n; i++)
for( j = 0; j < n; j++)
sum++;
The running time is O(n2)
Counting the number of ope
rations
C.
Consecutive program fragments Total
running time :
the maximum of the running time of the
individual fragments
Consecutive program fragme
nts
sum = 0;
for( i = 0; i < n; i++) O(n)
sum = sum + i;
sum = 0;
for( i = 0; i < n; i++)
for( j = 0; j < 2n; j++) O(n2)
sum++;
The maximum is O(n2)
Counting the number of
operations D:
If statement
if (C )
S1;
O(Max(S1,S2))
else
S2;
The running time is the maximum of the
running times of S1 and S2.
EXAMPLES
sum = 0;
for( i = 0 ; i < n; i++)
O(n3)
for( j = 0 ; j < n*n ; j++ )
sum++;
EXAMPLES
sum = 0;
for( i = 0; i < n ; i++)
O(n2)
for( j = 0; j < i ; j++)
sum++;
EXAMPLES
for(j = 0; j < n*n; j++)
? O(n3logn)
compute_val(j);
The complexity of compute_val(x) is given
to be O(n*logn)
Search in an unordered
array of elements
for (i = 0; i < n; i++)
if (a[ i ] == x) O(n)
return 1;
return -1;
Search in a table n x m
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
O(n*m)
if (a[ i ][ j ] == x)
return 1 ;
return -1;