Skip to content

Instantly share code, notes, and snippets.

@sudhanshuptl
Last active June 11, 2018 12:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudhanshuptl/9ee8f359c90afb6fe4f7f60e6bf91d29 to your computer and use it in GitHub Desktop.
Save sudhanshuptl/9ee8f359c90afb6fe4f7f60e6bf91d29 to your computer and use it in GitHub Desktop.
Different Sorting Algorithms in Python
# Bubble sort in python
__author__ ='Sudhashu Patel'
class BubbleSort:
def __init__(self, ls=[5,3,6,2,4,7,4,3]):
self.ls = ls
self.length = len(self.ls)
def sort(self, reverse=False):
if reverse:
selecter = lambda x, y: x < y
else:
selecter = lambda x, y: x > y
for i in range(self.length-1):
for j in range(self.length-1-i):
if selecter(self.ls[j], self.ls[j+1]):
self.ls[j], self.ls[j+1] = self.ls[j+1], self.ls[j]
if not reverse:
print('List is Sorted :')
else:
print('List is reverse sorted')
def print(self):
print(", ".join([str(x) for x in self.ls]))
if __name__ == '__main__':
bubbleSort = BubbleSort([4,5,2,5,7,5,88,55,4,3,55,45,3,4,6,99])
bubbleSort.print()
bubbleSort.sort(reverse=True)
bubbleSort.print()
bubbleSort.sort()
bubbleSort.print()
# Insertion sort in python
__author__ ='Sudhashu Patel'
class InsertionSort:
def __init__(self, ls=[5,3,6,2,4,7,4,3]):
self.ls = ls
self.length = len(self.ls)
def sort(self, reverse=False):
if reverse:
selecter = lambda x, y: x > y
else:
selecter = lambda x, y: x < y
for i in range(1, self.length):
key_index = i
for j in range(0,i):
if selecter(self.ls[j], self.ls[key_index]):
temp = self.ls.pop(key_index)
self.ls.insert(j, temp)
# insert at desired position
if not reverse:
print('List is Sorted :')
else:
print('List is reverse sorted')
def print(self):
print(", ".join([str(x) for x in self.ls]))
if __name__ == '__main__':
insertionSort = InsertionSort([4, 5, 2, 5, 7, 5, 88, 55, 4, 3, 55, 45, 3, 4, 6, 99])
insertionSort.print()
insertionSort.sort(reverse=True)
insertionSort.print()
insertionSort.sort()
insertionSort.print()
# Selection sort in python
__author__ ='Sudhashu Patel'
class SelectionSort:
def __init__(self, ls=[5,3,6,2,4,7,4,3]):
self.ls = ls
self.length = len(self.ls)
def find_min_index(self, base_index):
'''
:param base_index:
:return: find index of minima from base index onword
'''
min_ = base_index
for i in range(base_index+1,self.length):
if self.ls[min_] > self.ls[i]:
min_ = i
return min_
def find_max_index(self, base_index):
'''
:param base_index:
:return: find index of minima from base index onword
'''
max_ = base_index
for i in range(base_index+1,self.length):
if self.ls[max_] < self.ls[i]:
max_ = i
return max_
def sort(self, reverse=False):
if not reverse:
selecter = self.find_min_index
else:
selecter = self.find_max_index
for i in range(0, self.length):
selected_node = selecter(i)
#swap data
self.ls[i], self.ls[selected_node] = self.ls[selected_node], self.ls[i]
if not reverse:
print('List is Sorted :')
else:
print('List is reverse sorted')
def print(self):
print(", ".join([str(x) for x in self.ls]))
if __name__ == '__main__':
selectionSort = SelectionSort([4, 5, 2, 5, 7, 5, 88, 55, 4, 3, 55, 45, 3, 4, 6, 99])
selectionSort.print()
selectionSort.sort(reverse=True) #reverse sort
selectionSort.print()
selectionSort.sort()
selectionSort.print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment