Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions docs/Chap09/9.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ Pre-calculate the positions of the quantiles in $O(k)$, we use the $O(n)$ select
```cpp
PARTITION(A, p, r)
x = A[r]
i = p
for k = p to r
i = p - 1
for k = p to r - 1
if A[k] < x
i = i + 1
swap A[i] with A[k]
Expand All @@ -99,7 +99,7 @@ RANDOMIZED-SELECT(A, p, r, i)
if i == k
return q, A[q]
if i < k
r = q
r = q - 1
else
p = q + 1
i = i - k
Expand All @@ -111,6 +111,8 @@ k-QUANTITLES-SUB(A, p, r, pos, f, e, quantiles)
return
mid = (f + e) / 2
q, val = RANDOMIZED-SELECT(A, p, r, pos[mid])
if mid - 1 >= f
k-QUANTILES-SUB(A, p, q, pos, f, mid - 1, quantiles)
quantiles[mid] = val
k = q - p + 1
for i = mid + 1 to e
Expand All @@ -120,15 +122,18 @@ k-QUANTITLES-SUB(A, p, r, pos, f, e, quantiles)

```cpp
k-QUANTITLES(A, k)
if k == 1
return []

num = A.size() / k
mod = A.size() % k
pos = num[1..k]
for i = 1 to mod
pos[i] = pos[i] + 1
for i = 1 to k
pos = [1..(k-1)]
for i = 1 to k - 1
pos[i] = i <= mod ? num + 1 : num
for i = 2 to k - 1
pos[i] = pos[i] + pos[i - 1]
quantiles = [1..k]
k-QUANTITLES-SUB(A, 0, A.length, pos, 0, pos.size(), quantiles)
quantiles = [1..(k-1)]
k-QUANTITLES-SUB(A, 1, A.length, pos, 1, pos.size(), quantiles)
return quantiles
```

Expand Down