4
S1) k =0, I =1, j =1 for I =1, and j = 1. k =3 k =3, I = 2, j =2 for I=1 and j=1. K =6 k = 6, I =3, j=3 for i=3, j=3. K= 9 k = 9, I =4, j=4 for i=4, j=4. K= 12 k = 12, I =5, j=5 for i=5, j=5. K= 15 k = 15, I =6, j=6 for i=6, j=6 and J>5 so k = 15 + 6 = 21 K = 21 at the end of the excecution. The execution time is o (n+1), when n is the number of time a for loop running. 1) and 4)

Algorithm Assignment Help

Embed Size (px)

Citation preview

Page 1: Algorithm Assignment Help

S1) k =0, I =1, j =1 for I =1, and j = 1. k =3

k =3, I = 2, j =2for I=1 and j=1. K =6

k = 6, I =3, j=3for i=3, j=3. K= 9

k = 9, I =4, j=4for i=4, j=4. K= 12

k = 12, I =5, j=5for i=5, j=5. K= 15

k = 15, I =6, j=6for i=6, j=6 and J>5 so k = 15 + 6 = 21

K = 21 at the end of the excecution. The execution time is o (n+1), when n is the number of time a for loop running.

1) and 4)

Page 2: Algorithm Assignment Help
Page 3: Algorithm Assignment Help

3) Consider a 2D array i.e. the array which has n rows and m columns. The Algorithm to achieve o(n) times can be achieved as follows

Page 4: Algorithm Assignment Help

Array A = { {0, 1, 2, 3}, K = 0                     {3, 2, 1, 0}, L = 0

                     {3, 5, 6, -1}, M = -1

                     {3, 8, 3, 4}  }; N = 3 X = -1

lets consider n rows and m columns. So A11 … Anm elements exists in an array.

I) For each rows assign an integer K for A1m , L for A2m, M for A3m, N for A4mII) Compare A11 to A12 if A11 is lesser value assign K with A11 else Assign A12III) now compare K to A13 and A14 assign the lowest value to K, Hence K has the lowest value of First row. Value of K is 0IV) Follow the same for the other rows and assign the lowest value to L, M and N. here the value of L is 0, M is -1 and N is 3V) now define an integer X. compare K and L, assign the lowest value to XVI) Compare X to M and if M is highest compare X to N else compare M to N. the least value is stored in X i.e. -1.