Skip to content

Commit

Permalink
Merge pull request #10 from nabijaczleweli/master
Browse files Browse the repository at this point in the history
Fix syntax highlighting in readme.
  • Loading branch information
orlp committed Apr 19, 2018
2 parents 979c0a0 + 078fb4a commit 0887902
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions readme.md
Expand Up @@ -8,7 +8,7 @@ All code is available for free under the zlib license.

Best Average Worst Memory Stable Deterministic
n n log n n log n log n No Yes

### Usage

`pdqsort` is a drop-in replacement for [`std::sort`](http://en.cppreference.com/w/cpp/algorithm/sort).
Expand Down Expand Up @@ -69,13 +69,15 @@ Mispredictions don't affect Quicksort" by Stefan Edelkamp and Armin Weiss. In sh
branch predictor by using small buffers (entirely in L1 cache) of the indices of elements that need
to be swapped. We fill these buffers in a branch-free way that's quite elegant (in pseudocode):

buffer_num = 0; buffer_max_size = 64;
for (int i = 0; i < buffer_max_size; ++i) {
// With branch:
if (elements[i] < pivot) { buffer[buffer_num] = i; buffer_num++; }
// Without:
buffer[buffer_num] = i; buffer_num += (elements[i] < pivot);
}
```cpp
buffer_num = 0; buffer_max_size = 64;
for (int i = 0; i < buffer_max_size; ++i) {
// With branch:
if (elements[i] < pivot) { buffer[buffer_num] = i; buffer_num++; }
// Without:
buffer[buffer_num] = i; buffer_num += (elements[i] < pivot);
}
```

This is only a speedup if the comparison function itself is branchless, however. By default pdqsort
will detect this if you're using C++11 or higher, the type you're sorting is arithmetic (e.g.
Expand Down Expand Up @@ -114,4 +116,4 @@ Where n is the number of elements, and p is the percentile of the pivot after pa
`T(n, 1/2)` is the best case for quicksort. On modern systems heapsort is profiled to be
approximately 1.8 to 2 times as slow as quicksort. Choosing p such that `T(n, 1/2) / T(n, p) ~= 1.9`
as n gets big will ensure that we will only switch to heapsort if it would speed up the sorting.
p = 1/8 is a reasonably close value and is cheap to compute on every platform using a bitshift.
p = 1/8 is a reasonably close value and is cheap to compute on every platform using a bitshift.

0 comments on commit 0887902

Please sign in to comment.