-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.py
More file actions
59 lines (41 loc) · 756 Bytes
/
sort.py
File metadata and controls
59 lines (41 loc) · 756 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
49
50
51
import copy
def insert_sort(l):
if len(l)==0: pass
for j in range(1,len(l)):
key = l[j]
i = j-1
while ( i>=0 and l[i]>key ):
l[i+1] = l[i]
l[i] = key
i=i-1
def selection_sort(l):
for j in range(0, len(l)-1):
idex = j
i = j+1
while ( i<=len(l) -1):
if l[i] < l[idex]: idex = i
i = i+1
if j != idex:
t = l[idex]
l[idex] = l[j]
l[j] = t
def merge_sort(l,p,r):
if (p<r):
q = (r+p)/2
merge_sort(l,p,q)
merge_sort(l,q+1,r)
merge(l,p,q,r)
def merge(l,p,q,r):
L1 = copy.deepcopy(l[p:(q+1)])
L2 = copy.deepcopy(l[(q+1):(r+1)])
L1.append(999999)
L2.append(999999)
i = 0
j = 0
for k in range(p,r+1):
if(L1[i] < L2[j]):
l[k] = L1[i]
i = i + 1
else:
l[k] = L2[j]
j = j + 1