-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.py
More file actions
51 lines (46 loc) · 1.39 KB
/
Copy pathsorting.py
File metadata and controls
51 lines (46 loc) · 1.39 KB
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
__author__ = 'zjb'
def ssort(data):
# k is the number of elements sorted so far
# thus, k is the index where we will put the next one
for k in range(len(data)-1):
minindex = k # initially k'th item is "minimum so far" of remaining elements
for index in range(k+1,len(data)):
if data[index] < data[minindex]:
minindex = index
# now swap the min value into slot k
temp = data[k]
data[k] = data[minindex]
data[minindex] = temp
#print(data)
# no return, data is sorted in-place!
def msort(data):
if (len(data) == 1):
return data
midindex = len(data)//2
first = msort(data[0:midindex])
second = msort(data[midindex:len(data)])
# zip 'em up
ans = []
firstind = 0
secondind = 0
while firstind < len(first) and secondind < len(second):
if first[firstind] < second[secondind]:
ans.append(first[firstind])
firstind += 1
else:
ans.append(second[secondind])
secondind += 1
# one array is used up
if firstind < len(first):
ans.extend(first[firstind:])
else:
ans.extend(second[secondind:])
return ans
nums = [4, 7, 2, 1, 8, 3, 6]
print(nums)
ssort(nums)
print(nums)
nums = [4, 7, 2, 1, 8, 3, 6]
print(nums)
sortednums = msort(nums)
print(sortednums)