0% found this document useful (0 votes)
8 views3 pages

DAA Notes Ex

Design and Analysis of Algorithm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

DAA Notes Ex

Design and Analysis of Algorithm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
DESIGAN & ANYALYSIS OF ALGORITHM T* iteration | 2""iteration | 3" iteration afl==al2] | a2] =al3] a{]==al3] | al2}==al4] a{t]==al4] Observe the following facts from the above table: > The index variable i start from 0 and goes up to 3 as shown in dotted lines. ie, 1 €0t03 i.e. € 0 to 5-2 (n-2) ie, i € 0 ton-J > For each initial value of i, the index variable j always start from i+] and goes up to 4 as shown in the above example. ie,j €1t04 ie, i € 1 to S-1 (0-1) 1 € Oto nl > If afi] is same as afj], then duplicating item found and return 0. Algorithm Unique Element(a[],n) /Purpose: _,. Check whether the elements in a given array are distinct. /Mnputs:.“_ n- the number of items present in the table, a+», The array consisting of n elements. Output: return 0 if elements not unique (i.e., elements are duplicated) return 0 if elements unique (i.e., no elements are duplicated) for € 0 to'n-2 do /see numbers 1 the example for j € itl to n-1 do —_//see numbers 2 the example if(alil==alj]) // see numbers 3 the example return 0; Helements are not unique KLES GH BCA COLLEGE HAVERI Page 18 DESIGAN & ANYALYSIS OF ALGORITHM end for end for return 1; Helements are unique The time efficiency can be calculated as shown below Analys he parameter to be considered is n which represent the size of the input. = a{j] return 0” which is the innermost for loop. Stepl: Step2: The basic operation in the statement “if afi Step3: The number of comparisons not only depends on the value of n but depends on the position of repeated elements if present. The repeated elements if not present, the number of comparisons are more. So, let us find the worst case time efficiency and best case time efficiency. Worst ease efficiency: fori € Oto n-2.do forj ©i#1 tom do iftatiIe=ati) | eo-oe 2-22 v n-2 1 Note: upper bound=n-1 Lower bound=i+1 _ Y fn) = vi j=itl i=0 5 ; fn) = D2) - Ge at i=0 22 fo)=onaasi-1+1 n-2 fn) = Yin 1-i i=0 Replacing i by 0, 1, 2, ....+4 m-2 we get = (0-1) + (0-2) + (9-3) + eee BEZEL usin a KLES GH BCA COLLEGE HAVERI Page 19

You might also like