Skip to content

Commit bea3421

Browse files
committed
Added selection sort
1 parent 4df07ba commit bea3421

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pygorithms/sorting/selection_sort.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Author: OMKAR PATHAK
2+
# Created On: 31st July 2017
3+
4+
# Best O(n^2); Average O(n^2); Worst O(n^2)
5+
6+
# selection sort algorithm
7+
def sort(List):
8+
for i in range(len(List) - 1): #For iterating n - 1 times
9+
minimum = i
10+
for j in range( i + 1, len(List)): # Compare i and i + 1 element
11+
if(List[j] < List[minimum]):
12+
minimum = j
13+
if(minimum != i):
14+
List[i], List[minimum] = List[minimum], List[i]
15+
return List
16+
17+
# time complexities
18+
def bestcase_complexity():
19+
return 'O(n ^ 2)'
20+
21+
def averagecase_complexity():
22+
return 'O(n ^ 2)'
23+
24+
def worstcase_complexity():
25+
return 'O(n ^ 2)'
26+
27+
# easily retrieve the source code of the sort function
28+
def get_code():
29+
import inspect
30+
return inspect.getsource(sort)

0 commit comments

Comments
 (0)