---
# System prepended metadata

title: 'My Solution:'

---

# My Solution: 

> count = 0
> def binary_search(sorted_arr, key, length):
>     global count
>     print (sorted_arr)
>     count =  count + 1
>     midpt = int(len(sorted_arr)/2)
>     if key == sorted_arr[midpt]:
>         print("Found")
>     elif count > length:
>         print("Not Found")
>     elif key < sorted_arr[midpt]:
>         binary_search(sorted_arr[:midpt], key, length)
>     else:
>         binary_search(sorted_arr[midpt+1:], key, length)
>     
> target = [1,2,3,4,4,5,6,7,7,7,8,9]
> binary_search(target, 10, len(target))
> print(count)

# Problem here is that the Max Recursion can occur in some cases where the input is large enough, which can be a problem and we need to avoid that. Max Recursion check is there but that check will still wait till the complete arrray is not finished. Better is to use a fact the N/2 > Log(n)

===============================================================
# Defined Solution

> def binary_search(arr, low, high, x):
>  
>     #Check base case
>     
>     if high >= low:
>  
>         mid = (high + low) // 2
>  
>         # If element is present at the middle itself
>         if arr[mid] == x:
>             return mid
>  
>         # If element is smaller than mid, then it can only
>         # be present in left subarray
>         elif arr[mid] > x:
>             return binary_search(arr, low, mid - 1, x)
>  
>         # Else the element can only be present in right subarray
>         else:
>             return binary_search(arr, mid + 1, high, x)
>  
>     else:
>         #Element is not present in the array
>         return -1
>  
> arr = [ 2, 3, 4, 10, 40 ]
> x = 10
> result = binary_search(arr, 0, len(arr)-1, x)


# Recursive Binary Search

> def bin_ser_recursive(arr, k):
>     l = len(arr)//2
>     print(arr)
>     if l == 0:
>         return False
>     if k == arr[l]:
>         return True
>     elif k < arr[l]:
>         return bin_ser_recursive(arr[:l], k)
>     else:
>         return bin_ser_recursive(arr[l+1:], k)
>     
> target = [1,2,3,4,4,5,6,7,7,7,8,9]
> print(bin_ser_recursive(target, 7))



def heap_sort(arr):
    j = 0
    max = arr[0]
    if len(arr) == 1:
        return arr
    print (arr)
    i = 1
    while i < len(arr):
        if max < arr[i]:
           max = arr[i]
           j = i
        i+=1
    arr[j] = arr[0]
    arr[0] = max
    print(arr[0])
    return [arr[0]] + heap_sort(arr[1:])
    
    
print(heap_sort([1,4,9,8,7,2,3,4 ,17 ,3 ,12 ,9 ,6]))
> # Opposite selection sort 