forked from GotoCode/mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.py
More file actions
192 lines (112 loc) · 2.58 KB
/
Copy pathsort.py
File metadata and controls
192 lines (112 loc) · 2.58 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#
# sort.py
#
import random
test_input = [5, 3, 2, 1, 4]
#
# swap - primitive op.
#
def swap(List, i, j):
temp = List[i]
List[i] = List[j]
List[j] = temp
#
# Insertion Sort - O(n^2)
#
#
# NOTE: this function sorts
# InpList in-place...
#
def insertsort(InpList):
for i in range(0, len(InpList)):
j = i
while (j > 0) and (InpList[j] <= InpList[j-1]):
swap(InpList, j, j-1)
j = j - 1
# no return statement: in-place sorting.
#
# Helper function (for selectsort)
#
#
# returns index of minimum element
# in list 'List', starting
# from index 'start'
#
#
# REQUIRES: 0 <= start <= len(List)
#
def indexOfMin(List, start):
minIndex = start
minElem = List[start]
for i in range(start, len(List)):
if List[i] < minElem:
minIndex = i
minElem = List[i]
return minIndex
#
# Selection Sort - O(n^2)
#
# NOTE: This function sorts inpList in-place...
#
def selectsort(InpList):
for i in range(0, len(InpList)):
j = indexOfMin(InpList, i)
swap(InpList, i, j)
# no return statement: in-place sorting.
#
# merge - mergesort helper
#
#
# REQUIRES: L1 & L2 are sorted
#
# ENSURES: res contains the merged version of L1 with L2
#
def merge(L1, L2, res):
if (len(L1) == 0):
res.extend(L2)
elif (len(L2) == 0):
res.extend(L1)
else:
hd_L1 = L1[0]
hd_L2 = L2[0]
if (hd_L1 <= hd_L2):
res.append(hd_L1)
merge(L1[1:], L2, res)
else:
res.append(hd_L2)
merge(L1, L2[1:], res)
#
# Mergesort - O(n*log(n))
#
#
# ENSURES: returns the sorted version of InpList
#
def mergesort(InpList):
if len(InpList) <= 1:
return InpList
else:
left = InpList[:len(InpList)/2]
right = InpList[len(InpList)/2:]
res = []
merge(mergesort(left), mergesort(right), res)
return res
#
# Quicksort - O(n*log(n)) expected
#
#
# ENSURES: returns the sorted version of InpList
#
def quicksort(InpList):
if (len(InpList) <= 1):
return InpList
else:
pivot = random.sample(InpList, 1)[0]
less = filter(lambda x:x < pivot, InpList)
equal = filter(lambda x:x == pivot, InpList)
greater = filter(lambda x:x > pivot, InpList)
less_sorted = quicksort(less)
greater_sorted = quicksort(greater)
res = less_sorted
res.extend(equal)
res.extend(greater_sorted)
return res