forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.py
More file actions
30 lines (24 loc) · 767 Bytes
/
selection_sort.py
File metadata and controls
30 lines (24 loc) · 767 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
# Author: OMKAR PATHAK
# Created On: 31st July 2017
# Best O(n^2); Average O(n^2); Worst O(n^2)
# selection sort algorithm
def sort(List):
for i in range(len(List) - 1): #For iterating n - 1 times
minimum = i
for j in range( i + 1, len(List)): # Compare i and i + 1 element
if(List[j] < List[minimum]):
minimum = j
if(minimum != i):
List[i], List[minimum] = List[minimum], List[i]
return List
# time complexities
def bestcase_complexity():
return 'O(n ^ 2)'
def averagecase_complexity():
return 'O(n ^ 2)'
def worstcase_complexity():
return 'O(n ^ 2)'
# easily retrieve the source code of the sort function
def get_code():
import inspect
return inspect.getsource(sort)