You are on page 1of 2

COMPUTER SCIENCE ASSIGNMENT-2

by S. Balaji BE CSE
1. Find the minimum element in the List
index 0 1 2 3 4 5 6
a 78 56 67 45 67 71 32

n = 7 (Total number of elements)


Initially min = 78
Iteration 1 2 3 4 5
i 1 2 3 4 5
a[i] 56 67 45 71 32
Min = 78 56 56 45 45 32

Min = 32

2. Insert an element X in the given list of elements.


index 0 1 2 3 4 5
a 12 29 38 41 51 55

n = 6 (Total number of elements)


x = 39 (Element to be inserted)
How many iteration you need to complete continue Iteration 1?

Iteration 1:
index 0 1 2 3 4 5 6
a 12 29 38 41 51 55

i=5 a[5] = 55
a[i+1] = a[i]
a[6] = 55
Iteration 2: if a[i] > x and i >= 0
index 0 1 2 3 4 5 6
a 12 29 38 41 51 55

i=4 a[4] = 51
a[i+1] = a[i]
a[5] = 51

Iteration 3: if a[i] > x and i >= 0


index 0 1 2 3 4 5 6
a 12 29 38 41 51 55

i=3 a[3] = 41
a[i+1] = a[i]
a[4] = 41

index 0 1 2 3 4 5 6
a 12 29 38 39 41 51 55

If it will have Iteration 4, according to the condition a[i] > x,


38 > 39 fails so without moving it will come out a[i+1] = a[i]
Therefore, it becomes a[3] = 39.

You might also like