Max And Min Using Divide And Conquer | A Helpful Line-by-Line Code Tutorial

In computer science, divide and conquer is an algorithm design paradigm based on multi-branched recursion. A divide-and-conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem.

import random

min = 99999
max = 0

def getLarger(a, b):
    if b > a:
        return b
    else:
        return a
    
def getSmaller(a, b):
    if b < a:
        return b
    else:
        return a

def MinMax(array, start, end):
    global min
    global max
    if end - start > 2:
        MinMax(array, start, (start + end)/2)
        MinMax(array, (start + end)/2, end)    
    else:
        array = array[start:end]
        if end - start == 1:
            array.append(array[0])
        max = getLarger(max, getLarger(array[0], array[1]))
        min = getSmaller(min, getSmaller(array[0], array[1]))

def main():
    array = list()
    length = 100
    for x in range (0, length):
        array.append(random.randint(0, length))
    print array
    MinMax(array, 0, length)
    print "Max is ", str(max)
    print "Min is ", str(min)

if __name__ == "__main__":
    main()