DS CSIT Lecture 04
DS CSIT Lecture 04
Data Structures
Lecture-04
Solution:
Here, int Sum = 0; taking one operation, for (j = 0; j < N; j++)
& Sum++; taking 3N + 2 operations, cout << Sum; taking 1
operation. Then N--; takes one operation.
So a total of 1 + (3N + 2) + 1 + 1 = 3N + 5.
CS 232: Data Structures 3
Today’s
Today’s Lecture
Lecture
• Algorithm Performance Calculation
– Empirical Method (Discussed Previously)
– Analytical Method (Discussed Previously)
– Visualization
• Running Time Classification
– Best Case
– Worst Case
– Average Case
• Space Efficiency (Space Complexity)
• Algorithm Correctness
• The graph above shows the best, worst, and average running times of an
algorithm.
– Running times for 20 inputs (data items) of same size, but in different order.
• The algorithm takes
– minimum time to process Input #13 , the best time.
– maximum time to process Input #5 , the worst time.
– average time of all the running times for 20 inputs is also shown in the graph.
// Example 1 // Example 2
// Expected maxNum = 24 // Expected maxNum = –4
int numbers[4] = {13, 4, 24, int numbers[4] ={-13, -4, -24,
7}; -7};
int maxNum1 = -1; int maxNum2 = -1;
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
if (numbers[i] > if (numbers[i] > maxNum2)
maxNum1) maxNum2 =
maxNum1 = numbers[i]; }
numbers[i];//}Example 3: Expected maxNum = -4
cout<<"Max="<<maxNum2;
int numbers[4] ={-13, -4, -24, -7};
cout<<"Max="<<maxNum1
; int maxNum3 = numbers[0];
for (int i=0; i<4; i++) {
if (numbers[i] > maxNum3)
maxNum3 = numbers[i]; }
cout<<"Max="<<maxNum3;
CS 232: Data Structures 16
Summary
Summary
• Algorithm Performance Calculation
– Empirical Method (Discussed Previously)
– Analytical Method (Discussed Previously)
– Visualization
• Running Time Classification
– Best Case
– Worst Case
– Average Case
• Space Efficiency (Space Complexity)
• Algorithm Correctness