8000 add selection sort · AllAlgorithms/python@d89e7d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d89e7d3

Browse files
committed
add selection sort
1 parent 62e8655 commit d89e7d3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

sorting/selection_sort.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import print_function
2+
3+
4+
def selection_sort(collection):
5+
6+
length = len(collection)
7+
for i in range(length):
8+
least = i
9+
for k in range(i + 1, length):
10+
if collection[k] < collection[least]:
11+
least = k
12+
collection[least], collection[i] = (
13+
collection[i], collection[least]
14+
)
15+
return collection
16+
17+
18+
if __name__ == '__main__':
19+
try:
20+
raw_input # Python 2
21+
except NameError:
22+
raw_input = input # Python 3
23+
24+
user_input = raw_input('Enter numbers separated by a comma:\n').strip()
25+
unsorted = [int(item) for item in user_input.split(',')]
26+
print(selection_sort(unsorted))

0 commit comments

Comments
 (0)
0