Finding min-max Recursive

Our Objective

To implement a program to find out the largest and smallest number from a list / array using recursive divide and conquer algorithm.

 

The Theory

The given program is an implementation of the "maximum and minimum" problem using a divide-and-conquer approach. The goal is to find both the maximum and minimum elements in a given list 'a' within a specified range.

The program defines a function called 'maxmin' that takes three parameters: the list 'a', the starting index 's', and the ending index 'e'. It uses a helper function to recursively divide the list into smaller sub lists until it reaches the base case, which is when 's' is equal to 'e' (indicating a sublist with only one element).

In the base case, the function creates a temporary list 't' with two elements. Both elements are set to the single element in the sub list 'a[s]', representing both the 'maximum' and 'minimum' value. This sub list is then returned as the result.

In the recursive case, the function calculates the middle index 'mid' as the floor division of the sum of 's' and 'e' divided by 2. It then recursively calls 'maxmin' on the left and right halves of the list, i.e., from 's' to 'mid' and from 'mid + 1' to 'e', respectively.

Once the recursive calls return, the function compares the maximum and minimum values of the left and right sub lists. If the maximum value of the left sub list (lmaxmin[0]) is greater than the maximum value of the right sub list (rmaxmin[0]), it checks if the minimum value of the left sub list (lmaxmin[1]) is greater than the minimum value of the right sub list (rmaxmin[1]). If both conditions are true, it sets the result (res) to have the maximum value from the right sub list and the minimum value from the left sub list.

If the maximum value of the left sub list is not greater than the maximum value of the right sub list, it sets the result to have the maximum value from the right sub list and the maximum value from the left sub list.

If the minimum value of the left sublist is not greater than the minimum value of the right sublist, it sets the result to have the minimum value from the left sub list and the maximum value from the right sub list.

Finally, the function returns the result containing the maximum and minimum values found in the given sub list.

It's important to note that the program assumes the existence of a global list 't' and 'a' global list 'res', which are not explicitly defined in the code provided. These lists should be defined before calling the 'maxmin' function in order for the program to work correctly.

Divide and Conquer strategy

The divide and conquer strategy is a problem-solving approach in computer science and mathematics that involves breaking down a complex problem into smaller sub problems, solving them independently, and then combining the solutions to solve the original problem. The name comes from the three main steps involved:

  1. Divide: Break the original problem down into smaller, more manageable sub problems.
  2. Conquer: Solve the sub problems independently.
  3. Combine: Combine the solutions to the sub problems to solve the original problem.

The divide and conquer strategy is often used to solve problems that are difficult to solve directly, such as sorting algorithms, matrix multiplication, and finding the closest pair of points in a set.

The main advantage of the divide and conquer strategy is that it allows for parallel processing of the sub problems, which can result in faster processing times. Additionally, it can be easier to reason about and debug smaller sub problems rather than the original, larger problem.

Some common algorithms that use the Divide and Conquer strategy include:

  • Merge Sort: A sorting algorithm that divides the input array into two halves, sorts each half independently, and then merges the sorted halves back together.
  • Quick Sort: A sorting algorithm that partitions the input array into two sub arrays around a pivot value, recursively sorts the sub arrays, and then combines the sorted sub arrays.
  • Binary Search: A search algorithm that divides the input array in half at each step, reducing the search space until the desired value is found.

 

Learning Outcomes 

By learning the Divide and Conquer algorithm to find the maximum value of an array, you can achieve several learning outcomes, including:

  • Implementing Recursive Functions: You'll learn how to design and implement recursive functions to solve complex problems. The program uses recursion to split the list into sub lists and recursively calls the 'maxmin' function on these sub lists.
  • Analyzing and Solving Sub problems: The program requires analyzing sub problems and combining their solutions to solve the larger problem. You'll enhance your ability to break down complex problems into smaller, manageable parts and develop strategies to combine the solutions effectively.
  • Working with List Indexing and Slicing: The program utilizes list indexing and slicing to divide the input list into sub lists. You'll become familiar with these concepts and learn how to manipulate lists based on specific indices.
  • Comparing and Updating Values: The program involves comparing and updating values to determine the maximum and minimum elements within a range. You'll gain experience in comparing values and utilizing conditional statements to update variables accordingly.
  • Handling Base Cases: Understanding how to handle base cases is crucial in recursive programming. The program checks for the base case where the sub list has only one element, and returns the maximum and minimum values directly without further recursion.
  • Implementing Helper Functions: The program suggests the presence of helper functions such as 'maxmin' to encapsulate the recursive logic. You'll learn the benefits of using helper functions to modularize code and improve readability.
  • Familiarity with Global Variables: The program assumes the existence of global variables 't' and 'res'. You'll gain an understanding of how global variables can be used to store and update values across multiple function calls.
  • Analyzing Time Complexity: By studying the program, you can analyze its time complexity. The divide-and-conquer approach typically has a time complexity of O(n log n) for problems like finding the maximum and minimum elements.
  • Debugging and Problem-Solving Skills: As you explore and potentially execute the program, you may encounter errors or unexpected behavior. This presents an opportunity to practice debugging techniques and enhance your problem-solving skills.