class StoogeSort(): def __init__(self,data,filename): self.test = Plotter("Stooge Sort") self.data = data self.stoogesort(0,len(self.data)-1) self.test.animate(self.data,filename) def stoogesort(self,l,h): if l >= h: return if self.data[l]>self.data[h]: t = self.data[l] self.test.plot(self.data,l,h) self.data[l] = self.data[h] self.data[h] = t self.test.plot(self.data,h,l) if h-l + 1 > 2: t = (int)((h-l + 1)/3) self.stoogesort( l, (h-t)) self.stoogesort( l + t, (h)) self.stoogesort( l, (h-t))
class CombSort(): def __init__(self,data,filename): self.test = Plotter("Comb Sort") self.data = data self.combSort(len(self.data)) self.test.animate(self.data,filename) def getNextGap(self,gap): gap = (gap * 10)//13 if gap < 1: return 1 return gap def combSort(self,n): gap = n swapped = True while gap !=1 or swapped == 1: gap = self.getNextGap(gap) swapped = False for i in range(0, n-gap): if self.data[i] > self.data[i + gap]: self.test.plot(self.data,i,i+gap) self.data[i],self.data[i+gap] = self.data[i+gap],self.data[i] self.test.plot(self.data,i,i+gap) swapped = True
class InsertionSort(): def __init__(self, data, filename): self.plotter = Plotter("Insertion Sort") self.insertion_sort(data) self.plotter.animate(self.insertion_sort(data), filename) def insertion_sort(self, data): for i in range(len(data)): temp = data[i] j = i - 1 while j >= 0 and temp < data[j]: #I want to show the graph with index j+1 highlighted green and j+1 red self.plotter.plot(data, j + 1, j) data[j + 1] = data[j] data[j] = temp #After swapping I want to show the graph with index j highlighted red and j+1 green self.plotter.plot(data, j, j + 1) j -= 1 data[j + 1] = temp return data
class CycleSort(): def __init__(self, data, filename): self.plotter = Plotter("Cycle Sort") self.plotter.animate(self.cycle_sort(data), filename) def cycle_sort(self, data): for cycleStart in range(0, len(data) - 1): item = data[cycleStart] pos = cycleStart for i in range(cycleStart + 1, len(data)): if data[i] < item: pos += 1 if pos == cycleStart: continue while item == data[pos]: pos += 1 data[pos], item = item, data[pos] self.plotter.plot(data, pos, cycleStart) while pos != cycleStart: pos = cycleStart for i in range(cycleStart + 1, len(data)): if data[i] < item: pos += 1 while item == data[pos]: pos += 1 data[pos], item = item, data[pos] self.plotter.plot(data, cycleStart, pos) return data
class PancakeSort(): def __init__(self, data, filename): self.plotter = Plotter("Pancake Sort") self.plotter.animate(self.pancakesort(data), filename) def flip(self, arr, i): start = 0 while start < i: self.plotter.plot(arr, i, start) arr[start], arr[i] = arr[i], arr[start] self.plotter.plot(arr, start, i) start += 1 i -= 1 def findMax(self, arr, n): mi = 0 for i in range(0, n): if arr[i] > arr[mi]: mi = i return mi def pancakesort(self, data): curr_size = len(data) while curr_size > 1: mi = self.findMax(data, curr_size) if mi != curr_size - 1: self.flip(data, mi) self.flip(data, curr_size - 1) curr_size -= 1 return data
class HeapSort(): def __init__(self, data, filename): self.test = Plotter("Heap Sort") self.data = data self.heapSort(len(self.data)) self.test.animate(self.data, filename) def heapiy(self, n, i): largest = i l = 2 * i + 1 r = 2 * i + 2 if l < n and self.data[i] < self.data[l]: largest = l if r < n and self.data[largest] < self.data[r]: largest = r if largest != i: self.test.plot(self.data, largest, i) self.data[i], self.data[largest] = self.data[largest], self.data[i] self.test.plot(self.data, i, largest) self.heapiy(n, largest) def heapSort(self, n): for i in range(n // 2 - 1, -1, -1): self.heapiy(n, i) for i in range(n - 1, 0, -1): self.test.plot(self.data, 0, i) self.data[i], self.data[0] = self.data[0], self.data[i] self.test.plot(self.data, i, 0) self.heapiy(i, 0)
class QuickSort(): def __init__(self, data, filename): self.test = Plotter("Quick Sort") self.data = data self.quickSort(0, len(self.data) - 1) self.test.animate(self.data, filename) def partition(self, low, high): i = (low - 1) # index of smaller element pivot = self.data[high] # pivot for j in range(low, high): if self.data[j] <= pivot: i = i + 1 self.test.plot(self.data, j, i) self.data[i], self.data[j] = self.data[j], self.data[i] self.test.plot(self.data, i, j) self.test.plot(self.data, i + 1, high) self.data[i + 1], self.data[high] = self.data[high], self.data[i + 1] self.test.plot(self.data, high, i + 1) return i + 1 def quickSort(self, low, high): if low < high: pi = self.partition(low, high) self.quickSort(low, pi - 1) self.quickSort(pi + 1, high)
class CocktailSort(): def __init__(self,data,filename): self.test = Plotter("Cocktail Sort") self.data = data self.cocktailSort(len(self.data)-1) self.test.animate(self.data,filename) def cocktailSort(self,n): swapped = True while swapped: swapped = False start=0 for i in range (start, n): if (self.data[i] > self.data[i + 1]) : swapped = True self.test.plot(self.data,i+1,i) self.data[i],self.data[i+1] = self.data[i+1],self.data[i] self.test.plot(self.data,i,i+1) if not swapped: break swapped = False n = n-1 for i in range(n-1, start-1, -1): if (self.data[i] > self.data[i + 1]): swapped = True self.test.plot(self.data,i+1,i) self.data[i],self.data[i+1] = self.data[i+1],self.data[i] self.test.plot(self.data,i,i+1) start = start + 1
class GnomeSort(): def __init__(self,data,filename): self.plotter = Plotter("Gnome Sort") self.plotter.animate(self.gnome_sort(data),filename) def gnome_sort(self,data): index = 0 n=len(data) while index < n: if index == 0: index = index + 1 self.plotter.plot(data,index,index-1) if data[index] >= data[index - 1]: index = index + 1 else: data[index], data[index-1] = data[index-1], data[index] self.plotter.plot(data,index-1,index) index = index - 1 return data
class CountSort(): def __init__(self,data,filename): self.plotter = Plotter("Count Sort") self.plotter.animate(self.count_sort(data),filename) def count_sort(self,data): array = [0]*(max(data)+1) result = [] self.plotter.plot(data,len(data)-1) for element in data: array[element] += 1 for index, element in enumerate(array): if element != 0: result.append(index) self.plotter.plot(result,len(result)-1) return result
class PigeonholeSort(): def __init__(self, data, filename): self.plotter = Plotter("Pigeonhole Sort") self.plotter.animate(self.pigeonholesort(data), filename) def pigeonholesort(self, data): my_min = min(data) my_max = max(data) size = my_max - my_min + 1 holes = [0] * size self.plotter.plot(data, len(data) - 1) for x in data: holes[x - my_min] += 1 i = 0 for count in range(size): while holes[count] > 0: holes[count] -= 1 data[i] = count + my_min self.plotter.plot(data, len(data) - 1, i) i += 1 return data
class BucketSort(): def __init__(self,data,filename): self.plotter=Plotter("Bucket Sort") self.plotter.animate(self.bucket(data,filename),filename) def insertionSort(self,arr,b,filename): arr2 = [] for i in range(len(arr)): if i == b: k=len(arr2) for j in arr[i]: arr2.append(j) for i in range(1,len(arr[b])): up = arr[b][i] j = i - 1 while j >=0 and arr[b][j] > up: self.plotter.plot(arr2,k+j+1,k+j) arr2[k+j+1]=arr2[k+j] arr2[k+j]=up arr[b][j + 1] = arr[b][j] arr[b][j]=up self.plotter.plot(arr2,k+j,k+j+1) j -= 1 arr[b][j + 1] = up return arr[b],arr2 def bucket(self,x,filename): arr = [] arr2 = [] arr3=[] x = list(map(lambda a:a/100,x)) slot_num = 10 for i in range(slot_num): arr.append([]) for j in x: index_b = int(slot_num * j) arr[index_b].append(j) self.plotter.plot(x,0) for i in arr: if len(i)!=0: arr2.append(i) for i in range(len(arr2)): arr2[i],arr3 = self.insertionSort(arr2,i,filename) return arr3
class MergeSort(): def __init__(self, data, filename): self.plotter = Plotter("Merge Sort") self.length = len(data) self.plotter.plot(data, 0) self.plotter.plot(data, 0) self.plotter.animate(self.merge_sort(data, 0), filename) def merge_lists(self, left_sublist, right_sublist, start): i, j = 0, 0 result = [] while i < len(left_sublist) and j < len(right_sublist): if left_sublist[i] <= right_sublist[j]: result.append(left_sublist[i]) i += 1 else: result.append(right_sublist[j]) j += 1 self.plotter.plot([0] * start + result + [0] * (self.length - start - len(result)), start + len(result) - 1) result += left_sublist[i:] self.plotter.plot([0] * start + result + [0] * (self.length - start - len(result)), start + len(result) - 1) result += right_sublist[j:] self.plotter.plot([0] * start + result + [0] * (self.length - start - len(result)), start + len(result) - 1) return result def merge_sort(self, input_list, start): if len(input_list) <= 1: return input_list else: midpoint = int(len(input_list) / 2) left_sublist = self.merge_sort(input_list[:midpoint], start) right_sublist = self.merge_sort(input_list[midpoint:], start + midpoint) return self.merge_lists(left_sublist, right_sublist, start)