Write a class sorting that provides the user a chance to choose the way to sort a list from bubble sorting, selection sort, insertion sort.
In [ ]:
class sorting(object):
'''Class to implement the following list sorting methods:-
-bubbleSort(list)
-insertionSort(list)
-selectionSort(list)
'''
def __init__(self,l):
self.unsorted = l
self.sorted = []
def bubbleSort(self,l = self.unsorted):
for i in xrange(len(l)):
for j in xrange(1,len(l)-i):
if l[j-1]>l[j]:
l[j],l[j-1] = l[j-1],l[j]
self.sorted = l
def insertionSort(self,l = self.unsorted):
for j in xrange(1,len(l)):
key = l[j]
i = j-1
while(i>-1 and l[i]>key):
l[i+1] = l[i]
i-=1
l[i+1] = key
self.sorted = l
def selectionSort(self,l = self.unsorted):
for i in xrange(len(l)):
k = i
for j in xrange(i,len(l)):
if l[j]<l[k]:
k = j
l[k],l[i] = l[i],l[k]
self.sorted = l