Professional Documents
Culture Documents
Tugas 3 Matdis Rafli Aljabar
Tugas 3 Matdis 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!
2).
a). Describe an algorithm for finding the first and second largest element in a list of
integer!
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.
9. compare 16 with 28, since 16 < 28, set max = 28, so the second largest
element in a list is 28
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)
i:=i+1
return pair
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
if a i = x then;
S : = {I}
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.
a) and b)
min := a1
max := a1
{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