Download presentation
Presentation is loading. Please wait.
Published bykavitha s Modified over 5 years ago
1
Mathematical Analysis of Non- recursive Algorithm PREPARED BY, DEEPA. B, AP/ CSE VANITHA. P, AP/ CSE
2
Steps in mathematical analysis of non-recursive algorithms Decide on parameter n indicating input size Identify algorithm‘s basic operation Determine worst, average, and best case for input of size n Set up summation for C(n) reflecting algorithm‘s loop structure Simplify summation using standard formulas
3
Example: Selection sort Input: An array A[0..n-1] Output: Array A[0..n-1] sorted in ascending order for i ← 0 to n-2 do min ← i for j = i + 1 to n – 1 do if A[j] < A[min] min ← j swap A[i] and A[min]
4
basic operation: comparison Inner loop: n-1 S(i) = Σ 1 = (n-1) – (i + 1) + 1 = n – 1 - i j = i+1 Outer loop: n-2 n-2 Op = Σ S(i) = Σ (n – 1 – i) = Σ (n – 1) - Σ i i = 0 i = 0 Basic formula: n Σ i = n(n+1) / 2 i = 0 Op = (n – 1 )(n -1 ) – (n-2)(n-1)/2 = (n – 1) [2(n – 1) – (n – 2)] / 2 = =(n – 1) n / 2 = O(n2)
5
Time efficiency of nonrecursive algorithms General Plan for Analysis Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best cases for input of size n Set up a sum for the number of times the basic operation is executed Simplify the sum using standard formulas and rules
6
Useful summation formulas and rules l i n 1 = 1+1+…+1 = n - l + 1 In particular, l i n 1 = n - 1 + 1 = n (n) 1 i n i = 1+2+…+n = n(n+1)/2 n 2 /2 (n 2 ) 1 i n i 2 = 1 2 +2 2 +…+n 2 = n(n+1)(2n+1)/6 n 3 /3 (n 3 ) 0 i n a i = 1 + a +…+ a n = (a n+1 - 1)/(a - 1) for any a 1 In particular, 0 i n 2 i = 2 0 + 2 1 +…+ 2 n = 2 n+1 - 1 (2 n ) (a i ± b i ) = a i ± b i ca i = c a i l i u a i = l i m a i + m+1 i u a i
7
Thank You
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.