Skip to content

Instantly share code, notes, and snippets.

@syedjafer
Created August 31, 2022 08:59
Show Gist options
  • Save syedjafer/c0a08d7f9bc5977dd9916386f963eae4 to your computer and use it in GitHub Desktop.
Save syedjafer/c0a08d7f9bc5977dd9916386f963eae4 to your computer and use it in GitHub Desktop.
def binary_search(arr, key, start, end):
if start == end:
if arr[start] > key:
return start
else:
return start+1
if start > end:
return start
mid = (start+end)//2
if arr[mid] < key:
return binary_search(arr, key, mid+1, end)
elif arr[mid] > key:
return binary_search(arr, key, start, mid-1)
else:
return mid
def insertion_sort(arr):
total_num = len(arr)
for i in range(1, total_num):
key = arr[i]
j = binary_search(arr, key, 0, i-1)
arr = arr[:j] + [key] + arr[j:i] + arr[i+1:]
return arr
sorted_array = insertion_sort([29, 10, 14, 37, 14])
print("Sorted Array : ", sorted_array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment