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