-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertionSortOperator.py
60 lines (53 loc) · 1.9 KB
/
insertionSortOperator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
import matplotlib.pyplot as plt
class insertionSortOperator:
def __init__(self,loadedArray, plot):
self.toSort = loadedArray
self.sorted = False
self.plot = plot
if len(loadedArray) < 2:
self.sorted = True
else:
self.propagate = 1
self.toSwitch = 0
self.toMove = self.toSort[self.propagate]
def performSlowSortStep(self):
if self.sorted:
return
if self.toSwitch < 0 or self.toSort[self.toSwitch] <= self.toMove:
#print("B")
self.toSort[self.toSwitch+1] = self.toMove
self.propagate += 1
if self.propagate == len(self.toSort):
self.sorted = True
return 1
self.toSwitch = self.propagate - 1
self.toMove = self.toSort[self.propagate]
return 0
else:
#print("A")
self.toSort[self.toSwitch + 1] = self.toSort[self.toSwitch]
self.toSwitch -= 1
return 0
def performFastSortStep(self):
while True:
if self.sorted:
return 1
if self.toSwitch < 0 or self.toSort[self.toSwitch] <= self.toMove:
#print("B")
self.toSort[self.toSwitch+1] = self.toMove
self.propagate += 1
if self.propagate == len(self.toSort):
self.sorted = True
return 1
self.toSwitch = self.propagate - 1
self.toMove = self.toSort[self.propagate]
return 0
else:
#print("A")
self.toSort[self.toSwitch + 1] = self.toSort[self.toSwitch]
self.toSwitch -= 1
def animateSortOperation(self, i):
for rect, h in zip(self.plot, self.toSort):
rect.set_height(h)
self.performFastSortStep()