-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortAlgorithm.py
More file actions
46 lines (40 loc) · 1.29 KB
/
SortAlgorithm.py
File metadata and controls
46 lines (40 loc) · 1.29 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 abc import abstractmethod
from threading import Thread
# =======================
# SortAlgorithm
# =======================
class SortAlgorithm:
def __init__(self, name, array):
"""コンストラクタ"""
self.name = name
self.array = array
self.steps = []
self.is_sorted = False
# インスタンス化と同時に別スレッドでソート開始
thread = Thread(target=self.__sort)
thread.start()
def swap(self, idx1, idx2):
"""スワップ"""
self.steps.append(((idx1, idx2), "Swap"))
self.array[idx1], self.array[idx2] = self.array[idx2], self.array[idx1]
def compare(self, idx1, idx2):
"""比較(gt)"""
self.steps.append(((idx1, idx2), "Compare"))
return self.array[idx1] > self.array[idx2]
def generator(self):
"""ステップのジェネレータ"""
idx = 0
while True:
if len(self.steps) > idx:
yield self.steps[idx]
idx += 1
elif self.is_sorted:
yield None
def __sort(self):
"""スレッド用ソート関数"""
self.sort()
self.is_sorted = True
@abstractmethod
def sort(self):
"""アルゴリズム本体"""
pass