Skip to content

Instantly share code, notes, and snippets.

View shakeelmajeed-work's full-sized avatar
💭
Studying

Shakeel Majeed shakeelmajeed-work

💭
Studying
View GitHub Profile
@shakeelmajeed-work
shakeelmajeed-work / selection_sort.py
Last active August 29, 2020 14:17
Selection Sort in Python 3!
Array = [18, 6, 66, 44, 9, 22, 14] #example
for i in range(len(Array)): #looping through till length of array
minindex = i #initial index of smallest element
''''whilst looping through whole array, find the minimum value'''
for j in range(i+1, len(Array)):
if Array[minindex]>Array[j]: #if value at minimum index is bigger than the value adjancent to it
minindex=j #change the minimum index
#after finding the smallest element, swap it with the first index(increases by 1 due to the first loop)
Array[i], Array[minindex] = Array[minindex], Array[i]
#step by step output
@shakeelmajeed-work
shakeelmajeed-work / binary_search.py
Created July 23, 2020 20:32
Binary Search in Python 3!
def dosearch(array, target):
min = 0
max = len(array) - 1
array = sorted(array)
while min<=max:
guess = (min+max) // 2
if array[guess] == target:
return guess
elif array[guess] < target:
min = guess + 1