Discrete Mathematics Lecture 7
2 Analysis of Algorithms Analyzing an algorithm Time complexity Space complexity Time complexity Running time needed by an algorithm as a function of the size of the input Denoted as T(N)
For analysing running time What do we mean by running time analysis? Determine how running time increases as the size of the problem increases Cost for each statement 1 unit time for arithmetic and logical operation,assignment and return Number of time each statement done By this two step we can find T(n) Asymptotic notation O-”big-oh” notation Upper bound for algorithm 3
Step1 int I = 0; Step2 while (I < n) Step3 { cout << “I is: " <<I; I++; } T(n)=3n+2 O(n) 4 stepcostNo. Of time N+1 32N
5 Step1 int i = 1; Step2 double sum = 0; Step3 while(i < n) Step4 { double value; cin >> value; sum = sum + value; i++; } Step5 double average = sum / i ; Step6 cout << "Average: " << average; T(n)=5n+1 => O(n) stepcostNo. Of time N 44N
6 Step1 for (int i = 0; i < n; i++) Step2 { cout << "i is " << i; } T(n)=4n+3 => O(n) _____________________________________ stepcostNo. Of time 13N+1 21N
Find big-oh for Nested loop for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { //some statments } 7