4 February 2016

finding maximum element of a list - divide and conquer approach



Here I share my Python implementation of divide and conquer approach of finding maximum element from a list :
"""
Divide and conquer approach to find the maximum from a list
"""

def find_max(li, left, right):
    if left == right:
        return li[left]
    mid = (left + right) / 2
    max1 = find_max(li, left, mid)
    max2 = find_max(li, mid+1, right)
    return max1 if max1 > max2 else max2
    
    
def main():
    li = [1, 5, 2, 9, 3, 7, 5, 2, 10]
    print "Maximum element of the list is", find_max(li, 0, len(li)-1)
    
    
if __name__ == "__main__":
    main()

1 comment:

  1. merkur 500 slot machine
    merkur 500 slot machine - 인카지노 This is a new machine that is based on our previous 메리트 카지노 주소 machine and not only offers a great selection of the most popular worrione casino slot machines

    ReplyDelete

Thank you for comment. We will try to enhance the quality of this website.