-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshaker-sort.py
More file actions
46 lines (35 loc) · 1.17 KB
/
Copy pathshaker-sort.py
File metadata and controls
46 lines (35 loc) · 1.17 KB
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
from timeit import default_timer as timer
class ShakerSort:
def __init__(self, array):
self.array = array
def sort(self):
min_index = 1
max_index = len(self.array) - 1
swapped = True
while swapped:
swapped = False
for i in range(max_index, min_index - 1, -1):
if self.array[i] < self.array[i-1]:
self.array[i], self.array[i-1] = self.array[i-1], self.array[i]
min_index = i + 1
swapped = True
for i in range(min_index, max_index + 1):
if self.array[i] < self.array[i-1]:
self.array[i], self.array[i-1] = self.array[i-1], self.array[i]
max_index = i - 1
swapped = True
def print_array(self):
for i in range(len(self.array)):
print(self.array[i], end=" ")
print()
def main():
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
start = timer()
s = ShakerSort(array)
s.print_array()
s.sort()
s.print_array()
elapsed_time = timer() - start
print(elapsed_time)
if __name__ == "__main__":
main()