-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortCodility.py
More file actions
48 lines (39 loc) · 923 Bytes
/
SortCodility.py
File metadata and controls
48 lines (39 loc) · 923 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
38
39
40
41
42
43
44
45
46
47
48
# Sort Codility
# Uses python3
# Selection ort
def selectionSort(A):
n = len(A)
for i in range(n):
num = i
for j in range(i+1,n):
if A[j]<A[num]:
num = j
A[i],A[num] = A[num], A[i]
return A
# Counting sort
def countingSort(A):
n = len(A)
C = [0] * (max(A)+1)
for elem in A:
C[elem] += 1
P = []
for i in range(len(C)):
for _ in range(C[i]):
P.append(i)
return P
# Insertion sort
def insertionSort(A):
n = len(A)
for i in range(1,len(A)):
num = A[i]
for k in range(i-1,-1,-1):
if A[k] > num:
A[k+1] = A[k]
else:
A[k+1] = num
break
return A
if __name__ == "__main__":
print(selectionSort([2,5,3,7,5]))
print(countingSort([2,5,3,7,5]))
print(insertionSort([2,5,3,7,5]))