The document describes an algorithm for finding the minimum and maximum values in an array. It starts by setting the initial minimum and maximum values based on whether the array length is odd or even. It then loops through the array, comparing elements in pairs to update the running minimum and maximum. The time complexity is analyzed, showing it is O(n) for even lengths and O(n-1) for odd lengths due to the different pairing strategies.
The document describes an algorithm for finding the minimum and maximum values in an array. It starts by setting the initial minimum and maximum values based on whether the array length is odd or even. It then loops through the array, comparing elements in pairs to update the running minimum and maximum. The time complexity is analyzed, showing it is O(n) for even lengths and O(n-1) for odd lengths due to the different pairing strategies.
The document describes an algorithm for finding the minimum and maximum values in an array. It starts by setting the initial minimum and maximum values based on whether the array length is odd or even. It then loops through the array, comparing elements in pairs to update the running minimum and maximum. The time complexity is analyzed, showing it is O(n) for even lengths and O(n-1) for odd lengths due to the different pairing strategies.
If n is odd then we start the loop at i = 2, and if n is even
then we start the loop at i = 3. Within the loop, we compare the next two elements to see which is larger and which is smaller. The larger is possibly the new maximum, and the smaller is possibly the new minimum. b) procedure clever smallest and largest(a[1], a[2], . . . , a[n] : integers) if n is odd then min := a[1] max := a[1] else if a[1] < a[2] then min := a[1] max := a[2] else min := a[2] max := a[1] if n is odd then i := 2 else i := 3 while i < n if a[i]< a[i+1] then smaller := a[i] bigger := a[i+1] else smaller := a[i+1] bigger := a[i] if smaller < min then min := smaller if bigger > max then max := bigger i := i + 2 { min is the smallest integer among the input, and max is the largest} c) If n is even, then pairs of elements are compared (first with second, third with fourth, and so on), which comparisons to determine d comparisons. If n is odd, then there are comparisons for the updates, for a total of (3n 3)/2