forked from algorhythms/Algo-Quicksheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapterSort.tex
432 lines (367 loc) · 12.5 KB
/
chapterSort.tex
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
\chapter{Sort}
\section{Introduction}
List of general algorithms:
\begin{enumerate}
\item Selection sort: invariant
\begin{enumerate}
\item Elements to the left of $i$ (including $i$) are fixed and in ascending order (fixed and sorted).
\item No element to the right of $i$ is smaller than any entry to the left of $i$ ($A[i] \leq\min(A[i+1:n])$.
\end{enumerate}
\item Insertion sort: invariant
\begin{enumerate}
\item Elements to the left of $i$ (including $i$) are in ascending order (sorted).
\item Elements to the right of $i$ have not yet been seen.
\end{enumerate}
\item Shell sort: h-sort using insertion sort.
\item Quick sort: invariant
\begin{enumerate}
\item $|A_p|..\leq..|..unseen..|..\geq..|$ maintain the 3 subarrays.
\end{enumerate}
\item Heap sort: compared to quick sort it is guaranteed $O(N \lg N)$, compared to merge sort it is $O(1)$ extra space.
\end{enumerate}
\section{Algorithms}
\subsection{Quick Sort}
\subsubsection{Normal pivoting}\label{section:pivot}
The key part of quick sort is pivoting:
\begin{python}
def pivot(self, A, i, j):
"""
pivoting algorithm:
| p | closed set | open set |
| closed set | p | open set |
"""
p = i
closed = p
for ptr in xrange(i, j):
if A[ptr] < A[p]:
closed += 1
A[ptr], A[closed] = A[closed], A[ptr]
A[closed], A[p] = A[p], A[closed]
return closed
\end{python}
Notice that this implementation goes $O(N^2)$ for arrays with all duplicates.
\textbf{Problem with duplicate keys}: it is important to stop scan at duplicate
keys (counter-intuitive); otherwise quick sort will goes $O(N^2)$ for the
array with all duplicate items, because the algorithm will put all items
equal to the $A[p]$ on \textbf{a single side}.
Example: quadratic time to sort random arrays of 0s and 1s.
\subsubsection{Stop-at-equal pivoting}
Alternative pivoting implementation with optimization for duplicated keys:
\begin{python}
def pivot_optimized(self, A, lo, hi):
"""
Fix the pivot as the 1st element
Scan from left to right and right to left simultaneously
Avoid the case that the algo goes O(N^2) with duplicated keys
"""
p = lo
i = lo
j = hi
while True:
while True:
i += 1
if i >= hi or A[i] >= A[lo]:
break
while True:
j -= 1
if j < lo or A[j] <= A[lo]:
break
if i >= j:
break
A[i], A[j] = A[j], A[i]
A[lo], A[j] = A[j], A[lo]
return j
\end{python}
\subsubsection{3-way pivoting}
3-way pivoting: pivot the array into 3 subarrays:
$|..\leq..|..=..|..unseen..|..\geq..|$
\begin{python}
def pivot_3way(self, A, lo, hi):
lt = lo-1 # pointing to end of array LT
gt = hi # pointing to the end of array GT (reversed)
v = A[lo]
i = lo # scanning pointer
while i < gt:
if A[i] < v:
lt += 1
A[lt], A[i] = A[i], A[lt]
i += 1
elif A[i] > v:
gt -= 1
A[gt], A[i] = A[i], A[gt]
else:
i += 1
return lt+1, gt
\end{python}
\subsection{Merge Sort}
TODO
\section{Properties}
\subsection{Stability}
Definition: a stable sort preserves the \textbf{relative order of items with equal keys} (scenario: sorted by time then sorted by location).
Algorithms:
\begin{enumerate}
\item Stable
\begin{enumerate}
\item Merge sort
\item Insertion sort
\end{enumerate}
\item Unstable
\begin{enumerate}
\item Selection sort
\item Shell sort
\item Quick sort
\item Heap sort
\end{enumerate}
\end{enumerate}
\textbf{Long-distance swap} operation is the key to find the unstable case during sorting.
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=.50]{stable_sort}}
\caption{Stale sort vs. unstable sort}
\label{fig:trie}
\end{figure}
\subsection{Sort Applications}
\begin{enumerate}
\item Sort
\item Partial quick sort (selection), k-th largest elements
\item Binary search
\item Find duplicates
\item Graham scan
\item Data compression
\end{enumerate}
\subsection{Considerations}
\begin{enumerate}
\item Stable?
\item Distinct keys?
\item Need guaranteed performance?
\item Linked list or arrays?
\item Caching system? (reference to neighboring cells in the array?
\item Usually randomly ordered array?
(or partially sorted?)\item Parallel?
\item Deterministic?
\item Multiple key types?
\end{enumerate}
$O(N\lg N)$ is the lower bound of comparison-based sorting; but for other
contexts, we may not need $O(N \lg N)$:
\begin{enumerate}
\item Partially-ordered arrays: insertion sort to achieve $O(N)$. \textbf{Number of inversions}: 1 inversion $=$ 1 pair of keys that are out
of order.
\item Duplicate keys
\item Digital properties of keys: radix sort to achieve $O(N)$.
\end{enumerate}
\subsection{Summary}
\begin{figure}[hbtp]
\centering
\subfloat{\includegraphics[scale=0.80]{sort_summary}}
\caption{Sort summary}
\label{fig:trie}
\end{figure}
\section{Partial Quicksort}
\subsection{Find $m$ smallest}
\runinhead{Heap-based solution.} $O(n \log m)$
\runinhead{Partial Quicksort} Then the $A[:m]$ is sorted $m$ smallest. The algorithm recursively sort the $A[i:j]$
The average time complexity is
\begin{eqnarray*}
F(n) = \left\{ \begin{array}{rl}
F(\frac{n}{2})+O(n) &\mbox{// if $\frac{n}{2} \geq m$} \\
2F(\frac{n}{2})+O(n) &\mbox{// otherwise}
\end{array} \right.
\end{eqnarray*}
Therefore, the complexity is $O(n+m \log m)$.
\begin{python}
def partial_qsort(self, A, i, j, m):
if i >= j: return
p = self.pivot(A, i, j)
self.partial_qsort(A, i, p, m)
if p+1 >= m: return
self.partial_qsort(A, p+1, j, m)
\end{python}
\subsection{Find $k$-th}
Use partial quick sort to find $k$-th smallest element in the unsorted array. The algorithm recursively sort the $A[i:j]$
The average time complexity is
\begin{align*}
F(n) &= F(n/2) + O(n) \\
&= O(n)
\end{align*}
\begin{python}
def find_kth(self, A, i, j, k):
if i >= j: return
p = self.pivot(A, i, j)
if p == k: return A[p]
if p > k: return self.find_kth(A, i, p, k)
else: return self.find_kth(A, p+1, j, k)
\end{python}
Pivoting see section - \ref{section:pivot}.
\section{Inversion}
If $a_i > a_j$ but $i<j$, then this is considered as 1 Inversion. That is, for an element, the count of other elements that are \textit{larger} than the element but appear \textit{before} it. This is the default definition.
There is also an alternative definition: for an element, the count of other elements that are \textit{samller} than the element but appear \textit{after} it.
\subsection{MergeSort \& Inversion Pair}
MergeSort to calculate the reverse-ordered paris. The only difference from a normal
merge sort is that - when pushing the 2nd half of the array to the place, you calculate
the inversion generated by the element $A_2[i_2]$ compared to $A_1[i_1:]$.
\begin{python}
def merge(A1, A2, A):
i1 = i2 =0
ret = 0
for i in xrange(len(A)):
if i1 == len(A1):
A[i] = A2[i2]
i2 += 1
elif i2 == len(A2):
A[i] = A1[i1]
i1 += 1
else:
# use array diagram to illustrate
if A1[i1] > A2[i2]: # push the A2 to A
A[i] = A2[i2]
i2 += 1
# number of reverse-ordered pairs
ret += len(A1) - i1
else:
A[i] = A1[i1]
i1 += 1
return ret
def merge_sort(a):
n = len(a)
if n == 1:
return 0
a1 = a[:n/2]
a2 = a[n/2:]
ret1 = merge_sort(a1)
ret2 = merge_sort(a2)
# merge not merge_sort
ret = ret1+ret2+merge(a1, a2, a)
return ret
\end{python}
\subsection{Binary Index Tree \& Inversion Count}
Given $A$, calculate each element's inversion number.
Construct a BIT (\ref{BIT}) with length $max(A)+1$. Let BIT maintains the index of values. Scan the element from left to right (or right to left depends on the definition of inversion number), and set the index equal val to 1. Use the prefix sum to get the inversion number.
\pyinline{get(end) - get(a)} get the count of number that appears \textit{before} $a$ (i.e. already in the BIT) and also \textit{larger} than $a$.
Possible to extend to handle duplicate number.
\\
Core clues:
\begin{enumerate}
\item BIT maintains \textbf{index of values} to count the number of at each value.
\item \pyinline{get(end) - get(a)} to get the inversion count of $a$.
\end{enumerate}
\begin{python}
def inversion(self, A):
bit = BIT(max(A)+1)
ret = []
for a in A:
bit.set(a, 1) # += 1 if possible duplicate
inversion = bit.get(max(A)+1) - bit.get(a)
ret.append(inversion)
return ret
\end{python}
\subsection{Segment Tree \& Inversion Count}\label{segmentTreeInversionCount}
Compared to BIT, Segment Tree can process queries of both $idx \rightarrow sum$ and $sum \rightarrow idx$; while BIT can only process $idx \rightarrow sum$.
Core clues:
\begin{enumerate}
\item Segment Tree maintains \textbf{index of values} to count the number of at each value.
\item \pyinline{get(root, end) - get(root, a)} to get the inversion count of $a$.
\end{enumerate}
\begin{python}
class SegmentTree(object):
def __init__(self):
self.root = None
def build(self, root, lo, hi):
if lo >= hi: return
if not root: root = Node(lo, hi)
root.left = self.build(root.left, lo, (lo+hi)/2)
if root.left:
root.right = self.build(root.right, (lo+hi)/2, hi)
return root
def set(self, root, i, val):
if root.lo == i and root.hi-1 == root.lo:
root.cnt_this += val
elif i < (root.lo+root.hi)/2:
root.cnt_left += val
self.set(root.left, i, val)
else:
self.set(root.right, i, val)
def get(self, root, i):
if root.lo == i and root.hi-1 == root.lo:
return root.cnt_left
elif i < (root.lo+root.hi)/2:
return self.get(root.left, i)
else:
return (
root.cnt_left + root.cnt_this +
self.get(root.right, i)
)
class Solution(object):
def _build_tree(self, A):
st = SegmentTree()
mini, maxa = min(A), max(A)
st.root = st.build(st.root, mini, maxa+2)
# maxa+1 is the end dummy
return st
def countOfLargerElementsBeforeElement(self, A):
st = self._build_tree(A)
ret = []
end = max(A)+1
for a in A:
ret.append(
st.get(st.root, end) - st.get(st.root, a)
)
st.set(st.root, a, 1)
return ret
\end{python}
\subsection{Reconstruct Array from Inversion Count}\label{inversionReconstruct}
Given a \textit{sorted} numbers with their associated inversion count (\# larger numbers before this element). $A[i].val$ is the value of the number, $A[i].inv$ is the inversion number. Reconstruct the original array $R$ that consists of each $A[i].val$.
Brute force can be done in $O(n^2)$. Put the $A[i].val$ into $R$ at an index/slot s.t. the \# \textit{empty} slots before it equals to $A[i].inv$.
\rih{BST}. Possible to use BST to maintain the empty slot indexes in the original array. Each node's rank indicates the count of empty indexes in its left subtree. But need to maintain the deletion.
\rih{Segment Tree}. Use a segment tree to maintain the size of empty slots. Each node has a $start$ and a $end$ s.t slot indexes $\in [start, end)$. Go down to find the target slot, go up to decrement the size of empty slots.
Reconstruction of array cannot use BIT since there is no map of $prefixSum \rightarrow i$.
\newpage
\begin{python}
class Node(object):
def __init__(self, start, end, cnt):
self.start = start
self.end = end
self.cnt = cnt
self.left = None
self.right = None
def __repr__(self):
return repr("[%d,%d)" % (self.start, self.end))
class SegmentTree(object):
"""empty space"""
def __init__(self):
self.root = None
def build(self, start, end):
"""a node can have right ONLY IF has left"""
if start >= end:
return
root = Node(start, end, end-start)
root.left = self.build(start, (end+start)/2)
if root.left:
root.right = self.build((start+end)/2, end)
return root
def find_delete(self, root, val):
"""
:return: index
"""
root.cnt -= 1
if not root.left:
return root.start
elif root.left.cnt >= val:
return self.find_delete(root.left, val)
else:
return self.find_delete(root.right,
val - root.left.cnt)
class Solution(object):
def reconstruct(self, A):
st = SegmentTree()
n = len(A)
st.root = st.build(0, n)
A = sorted(A, key=lambda x: x[0])
ret = [0]*n
for a in A:
idx = st.find_delete(st.root, a[1]+1)
ret[idx] = a[0]
return ret
if __name__ == "__main__":
A = [(5, 0), (2, 1), (3, 1), (4, 1,), (1, 4)]
assert Solution().reconstruct(A) == [5, 2, 3, 4, 1]
\end{python}