-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.py
More file actions
executable file
·36 lines (28 loc) · 957 Bytes
/
Copy pathselectionSort.py
File metadata and controls
executable file
·36 lines (28 loc) · 957 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
#!/usr/bin/env python3
import argparse
from code2dtree import func2dtree, getVarList
from code2dtree.nodeIO import printTree, saveTree, SAVE_FORMATS
from code2dtree.types import CompT
def selectionSorted(a: list[CompT]) -> list[CompT]:
a = a.copy()
n = len(a)
for i in range(n):
for j in range(i+1, n):
if a[i] > a[j]:
a[j], a[i] = a[i], a[j]
return a
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('n', type=int, help='number of elements in list to sort')
formats = ', '.join(SAVE_FORMATS)
parser.add_argument('-o', '--output', help=f'path to output file (formats: {formats})')
args = parser.parse_args()
a = getVarList('a', args.n)
print('input:', a)
dtree = func2dtree(selectionSorted, (a,))
if args.output is not None:
saveTree(dtree, args.output)
else:
printTree(dtree)
if __name__ == '__main__':
main()