This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |