-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsort.py
More file actions
174 lines (146 loc) · 4.36 KB
/
Copy pathsort.py
File metadata and controls
174 lines (146 loc) · 4.36 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
# sorting algorithms
import random
def swap(A, i, j):
if i != j:
A[i], A[j] = A[j], A[i]
# See: http://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation
def bubbleSort(A):
numComp = numAccess = 0
n = len(A)
swapped = True
while swapped and n > 1:
swapped = False
for i in range(1, n):
numAccess += 2
numComp += 1
if A[i-1] > A[i]:
numAccess += 2 # two writes by swap (A[i-1], A[i] already read)
swap(A, i-1, i)
swapped = True
n -= 1
return {
"number of compares" : numComp,
"number of accesses" : numAccess,
}
# See: http://en.wikipedia.org/wiki/Selection_sort
def selectionSort(A):
numComp = numAccess = 0
n = len(A)
for i in range(0, n-1):
k = i
numAccess += 1
a = A[k]
for j in range(i+1, n):
numAccess += 1
b = A[j]
numComp += 1
if b < a:
k = j
a = b
if i != k:
numAccess += 3 # two writes + read A[i] by swap
swap(A, i, k)
return {
"number of compares" : numComp,
"number of accesses" : numAccess,
}
# See: http://en.wikipedia.org/wiki/Insertion_sort#Algorithm
def insertionSort(A):
numComp = numAccess = 0
n = len(A)
for i in range(1, n):
numAccess += 1
a = A[i]
j = i
while j > 0:
numAccess += 1
b = A[j-1]
numComp += 1
if a < b:
numAccess += 1
A[j] = b
j -= 1
else:
break
numAccess += 1
A[j] = a
return {
"number of compares" : numComp,
"number of accesses" : numAccess,
}
# See: http://en.wikipedia.org/wiki/Quicksort#In-place_version
def quickSort(A):
global numComp, numAccess
numComp = numAccess = 0
def partition(A, left, right, pivotIndex):
global numComp, numAccess
numAccess += 4 # two writes + two reads by swap
pivotValue = A[pivotIndex]
swap(A, pivotIndex, right)
storeIndex = left
for i in range(left, right):
numAccess += 1
numComp += 1
if A[i] < pivotValue:
if i != storeIndex:
numAccess += 3 # two writes + one read by swap (A[i] already read)
swap(A, i, storeIndex) # TODO maybe we don't need swap here, just shift
storeIndex += 1
if storeIndex != right:
numAccess += 4 # two writes + two reads by swap
swap(A, storeIndex, right)
return storeIndex
def qsort(A, left, right):
if left < right:
pivotIndex = random.randint(left, right)
pivotNewIndex = partition(A, left, right, pivotIndex)
qsort(A, left, pivotNewIndex-1)
qsort(A, pivotNewIndex+1, right)
qsort(A, 0, len(A)-1)
return {
"number of compares" : numComp,
"number of accesses" : numAccess,
}
# See: http://en.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation
def mergeSort(C):
global numComp, numAccess
numComp = numAccess = 0
def merge(A, left, right, end, B):
global numComp, numAccess
i = left
j = right
for k in range(left, end):
numAccess += 2 # always one read (A[i] or A[j]) and a write (to B[k])
use_x = False
if i < right:
x = A[i]
if j >= end:
use_x = True
else:
numAccess += 1 # A[j] also read
y = A[j]
numComp += 1
if x <= y:
use_x = True
else:
y = A[j]
if use_x:
B[k] = x
i += 1
else:
B[k] = y
j += 1
A = C
N = len(A)
B = N * [None]
width = 1
while width < N:
for i in range(0, N, 2*width):
merge(A, i, min(i+width, N), min(i+2*width, N), B)
A, B = B, A
width *= 2
C[:] = A
return {
"number of compares" : numComp,
"number of accesses" : numAccess,
}