














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Bimala Kambhampat deliveered this lecture at Chhattisgarh Swami Vivekanand Technical University for course. It includes: Insertion, Sort, Elements, Linear, Position, Partitions, Array, Algorithm, Comparisons, Cost, Function
Typology: Slides
1 / 22
This page cannot be seen from the preview
Don't miss anything!















13 2145 79 47 22 38
74 3666 94
2957 81 60
16 (^45666045) the fourth iteration of this loop is shown here
An insertion sort partitions the array into two regions
outer^ loop outer times inner^ loop
inner^ times
n-1^ times ^ 5 instructions (including outer loop comparison and increment) ^ Total cost of the outer loop: 5(n-1) How many times the inner loop is evaluated is affected by thestate of the array to be sorted Best case: the array is already completely sorted so no “shifting”of array elements is required. ^ We only test the condition of the inner loop once (2 operations = 1comparison + 1 element comparison), and the body is neverexecuted ^ Requires 2(n-1) operations.
4i+ operations Therefore, the total cost of the inner loop will be
2n(n-1)+n- ^ Time cost:^ ^ Best case:^ 7(n-1)^ ^ Worst case:^ 5(n-1)+2n(n-1)+n-1 ^ What about the number of moves?^ ^ Best case:^ 2(n-1)
moves Worst case: 2(n-1)+n(n-1)/
E2 -> OK (do nothing) 1.2 If E1 > E2 then Swap(E1, E2) and set flag = true 3. repeat 1. and 2. while flag=true.
---- finish the first traversal ---- 1 1 2 23 9
---- finish the second traversal ----
n ) is large ^ Since cost functions are complex, and may bedifficult to compute, we approximate them using Onotation
(^2) t ( n ) = n + 20 n A
^ Which term dominates? It depends on the size of
n ^ n^ = 2,^ t ( n ) = 4 + 40 + 100A^ ^ The constant, 100, is the dominating term ^ n^ = 10,^ t ( n ) = 100 + 200 + 100A^ ^20 n^ is the dominating term ^ n^ = 100,^ t ( n ) = 10,000 + 2,000 + 100A^2 ^ n is the dominating term ^ n^ = 1000,^ t ( n ) = 1,000,000 + 20,000 + 100A^2 ^ n is the dominating term
t ( n)^ and^ g(n), w A^
e can say that the efficiency of an algorithm is
of order g(n)^ if there are positive constants
c^ and^ m^ such that ^ t (n) ·^ c.g ( n ) for allA^
n ¸ m ^ we write^ ^ t (n) is O( g ( n )) and we say thatA^ ^ t (n) is of orderA^
g ( n ) ^ e.g. if an algorithm’s running time is 3
n^ + 12 then the algorithm is O( n ). If
c^ is 3 and^ m^ is 12 then: ^ 4 *^ n^ ^3 n^ + 12 for all
n^ ^12
t(n),^ can be approximated A by another, simpler, function
g(n)^ which is also a function with only 1 variable, the data size
n. ^ The function^ g(n)^
is selected such that it represents an
upper bound^ on the efficiency of the algorithm A (i.e. an upper boundon the value of^ t(n) ).A This is expressed using the big-O notation: O
(g(n)). ^ For example, if we consider the time efficiency of algorithm Athen “ t (n) is O (g(n)) A
” would mean that ^ A cannot take more “time” than O
(g(n))^ to execute or that (more than^ c.g(n)^ for some constant
c ) ^ the cost function^ t(n) A
grows^ at most as^ fast as
g(n) docsity.com