Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Last active November 12, 2015 14:43
Show Gist options
  • Save krysseltillada/8f47f9028243a3c4d110 to your computer and use it in GitHub Desktop.
Save krysseltillada/8f47f9028243a3c4d110 to your computer and use it in GitHub Desktop.
python (Bubble_sort, Binary_search) implementation
class Algorithms: # wrap them as a class
def __init__ (self): # default method instantiation
return
def BubbleSort (self, param, sortMethod): # bubblesort implementation
i = 1
temp = 0
if (sortMethod == "ascending"):
for i in range(len(param)):
for a in range (len(param)):
if (param[i] > param[a]):
temp = param[a]
param[a] = param[i]
param[i] = temp
elif (sortMethod == "descending"):
for i in range(len(param)):
for a in range (len(param)):
if (param[i] < param[a]):
temp = param[a]
param[a] = param[i]
param[i] = temp
else:
return
return
def BinarySearch (self, param, value): # binary search implementation
min = 0; max = len (param) - 1; guess = 0
while (min < max):
guess = int (max + min / 2)
if (param[guess] == value):
return 1
elif(param[guess] < value):
min = guess + 1
else:
max = guess - 1
return 0
array = [1, 23, 44, 0, 123, 1551] # list of values
sort = Algorithms() # creates a default object
sort.BubbleSort (array, "descending") # sorts the array
for it in range(len(array)): # displays the values in an array
print (array[it])
if (sort.BinarySearch (array, 44)): # if value was found then
print ("value 44 was found")
else:
print ("value 44 was not found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment