NEP -DAA -DESIGN AND ANALYSIS OF ALOGRITHM

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7 PROGRAM 8 PROGRAM 9 PROGRAM 10 PROGRAM 11 PROGRAM 12

PART B

PROGRAM B1 PROGRAM B2 PROGRAM B3 PROGRAM B4 PROGRAM B5 PROGRAM B6 PROGRAM B7 PROGRAM B8 . . .

5.

 
  
 
 #5.Write a program to find minimum and maximum value in an array using divide and conquer.


def find_min_max(arr, low, high):
    # Base case: if there is only one element
    if low == high:
        return arr[low], arr[low]
    
    # Base case: if there are two elements, return min and max
    elif high == low + 1:
        if arr[low] < arr[high]:
            return arr[low], arr[high]
        else:
            return arr[high], arr[low]
    
    # Recurse on left and right halves
    mid = (low + high) // 2
    min1, max1 = find_min_max(arr, low, mid)
    min2, max2 = find_min_max(arr, mid + 1, high)
    
    # Combine results
    return min(min1, min2), max(max1, max2)

# Example usage
arr = [3, 5, 1, 8, 9, 2, 7, 6]
min_val, max_val = find_min_max(arr, 0, len(arr) - 1)
print("Minimum value:", min_val)
print("Maximum value:", max_val)