Download presentation
Presentation is loading. Please wait.
Published byJeremy Cannon Modified over 9 years ago
1
Advanced Algorithms Analysis and Design Lecture 6 (Continuation of 5 th Lecture) By Engr Huma Ayub Vine 1
3
Algorithm Comparison (5 elements)
30
Quicksort first call 2 nd call 3 rd call Each time the (sub) list is partitioned exactly one value (the pivot) is placed in its correct position. If the list is equally divided during each partitioning, it will require about lg (n) calls to quickSort to produce a sorted list.
36
Assignment 2 Prove that quick sort recursive case average analysis is Submit after midterm
37
Algorithm Comparison (2K Elements) 9
38
Computation Time for 100,000 elements 250 200 150 100 50 0 Series1
39
COMPARISON SortWorstAverageBest Method 22 Bubble O(n) )O(n) QuickO(n 2 )O(n log n)O(log n) HeapO(n log n) O(log n) 22 Insertion O(n) )O(n) 22 Selection O(n) )O(n) Merge O(n log n) O(log n) RadixO(n 2 )O(s*n)O(n log n) 25/3 Shell O(n) )O(n) CountingO(n + k)O(n) Bucket O(n 2 )O(n) PigeonholeO(n + s)O(n)
40
ALGORITHMS AND THEIR COMPLEXITY (Limits on Problem Size as Determined by Growth Rate) Algo Time Complexity Maximum Problem Size (n) 1 sec1 min1 hour A1n10006 x 10 4 3.6 x 10 6 A2n log n14048932.0 x 10 5 A3n2n2 312441897 A4 3 n 1039153 A52n2n 91521
41
Calculating the Greatest Common Divisor Function Euclid (m,n) while m > 0 do t ← m m ← n mod m n ← t return n Euclid (14, 21) :tm n 14 7 7 07 return = 7 Euclid (6, 15) :tm n 6 36 3 03 return = 3 Takes a time in the order of the algorithm of its arguments
42
Calculating the Fibonacci Sequence ƒ 0 = 0, ƒ 1 = 1 ƒ n = ƒ n-1 + ƒ n-2 for n > = 2 Function Fibrec (n) if n < 2 then return n else return Fibrec (n - 1) + Fibrec (n - 2) f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 01123581321 345589 Time required to calculate f n is in the order of the value of f n. It is very inefficient compared to de Moivre’s formula It is better to use Fibiter algorithm to calculate the Fibonnaci Sequence
43
Function Fibiter (n) { Calculates the n-th term of the Fibonacci sequence;} i ← 1; j ← 0 for k ← 1 to n do j ← i + j i ← j - i return j Fibonacci (5) i = 1 j = 0 k =12345 j =11235 i=01123 Time required to calculate f n is in the order of n
44
Calculating the Fibonacci Sequence de Moivre’s formula fn = 1 n - n [ φ− ( −φ ) ] 5 1+5 where φ= 2 ( φ=1.61803 ) Time required to calculate f n is value of f n is in the order of φ n
45
Example f10= f10-1 + f10-2 = f9 + f8 =34+21 =55----------------------(i) f 10= = 1n−n1n−n [ φ− ( −φ ) ] 5 1 10−10 [ 1.61803 ) − ( −1.61803 ) ] 2.236 = = 1 [ 1 [ 122.98883−0.008131 ] 122.9807 ] = 55.0003 = 55 (ii)
46
Function Fibiter (n) i ← 1; j ← 0 for k ← 1 to n d j ← i+j i ← j - i return j Comparison of modulo fibonacci algorithms n10203050100 Fibrec8 msec1 sec2 min21 days10 9 years Fibiter1/61/3½ msec¾ msec1 ½ msec msec
47
Important Points for an Algorithm Correct in execution Execution time Storage needs Limitation of computing equipment to support operations for desired numbers to have precision within in limits Efficient methodology for the specific task Programming/ hardware implementation tools Average/worst-case performance
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.