Question:- Describe an algorithm for finding both the largest and the smallest integers in a finite sequence of integers in an array and count them how many comparison operation are involved.
Answers
Answered by
1
FUNCTION MAX_MIN_COMPARE_Count ( A[0, N-1] , N)
Begin
// A[0, N-1] = integer array
integer j // = index of smallest
integer k // = index of largest
integer m // moving index
integer count //= of comparisons
m = 0 j = 0 k = 0 count = 0 m = 0
While m < N do
begin
count = count + 1
if A[m] < A[j] then
j = m
else
k = m
end if
m = m + 1
end while
print count
// A[j] is smallest and A[k] is the largest
END
Begin
// A[0, N-1] = integer array
integer j // = index of smallest
integer k // = index of largest
integer m // moving index
integer count //= of comparisons
m = 0 j = 0 k = 0 count = 0 m = 0
While m < N do
begin
count = count + 1
if A[m] < A[j] then
j = m
else
k = m
end if
m = m + 1
end while
print count
// A[j] is smallest and A[k] is the largest
END
Similar questions