Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Discrete Mathematics

NAME : M Rafli Aljabar

NIM : 09011381924118

CLASS : SK 3 Bukit

1).

a). Describe an algorithm for locating the last occurrence of the largest number in a
list on

integer!

1. create a list of integers with random number.

2. compare 16 with 9, since 16 > 9, set max = 16

3. compare 16 with 3, since 16 > 3, max is still 16

4. compare 16 with 1, since 16 < 36, set max = 36

5. compare 36 with 50, since 36 < 50, set max = 50

b). Estimate the number of comparisons used!

S = (16, 9, 3, 36, 50)

2).

a). Describe an algorithm for finding the first and second largest element in a list of
integer!

1. create a list of integers.

2. compare 8 with 16, since 8 < 16, set max = 16

3. compare 16 with 12, since 16 > 12 , max is till 16

4. compare 16 with 28, since 16 < 28, set max = 28

5. compare 28 with 51, since 28 < 51, set max = 51, so the first largest element in
a list is 51
6. compare between number 8, 16, 12, 28.

7. compare 8 with 16, since 8 < 16, set max = 16

8. compare 16 with 12, since 16 > 12, max is still 16

9. compare 16 with 28, since 16 < 28, set max = 28, so the second largest
element in a list is 28

b). Estimate the number of comparisons used!

S = (8, 16, 12, 28, 51)

3).

a). Give an algorithm to determine whether a bit string contains a pair of consecutive
zeros?

Procedure locate (a1, a2, a3, a4, ......, an : integer with n ≥ 1 and a1 ≥ a2, an, x :
integer)

While pair = false and i ≤ n

if si = 0 and si – 1 = 0, then pair : = true

i:=i+1

return pair

b). How many comparisons does the algorithm use?

4n – 4, 0 (n)

4).

a). Suppose that a list contains integers that are in order of largest to smallest and an

integer can appear repeatedly in this list. Devise an algorithm that locates all

occurrences of an integer x in the list.


for i : = 1 to n

if a i = x then;

S : = {I}

b). Estimate the number of comparisons used.

No comparisons.

5).

a). Adapt Algorithm 1 in Section 3.1 to find the maximum and the minimum of a
sequence of n elements by employing a temporary maximum and a temporary
minimum that is updated as each successive element is examined.

b). Describe the algorithm from part (a) in pseudocode.

a) and b)

procedure smallest and largest(a1, a2,...,an: integers)

min := a1

max := a1

for i := 2 to n if ai < min then min := ai

if ai > max then max := ai

{min is the smallest integer among the input, and max is the

largest}

c). How many comparisons of elements in the sequence are carried out by this
algorithm?

(Do not count comparisons used to determine whether the end of the sequence has

been reached.)

2n − 2

You might also like