Skip to content

Commit

Permalink
Add quick sort manual tail-call optimization
Browse files Browse the repository at this point in the history
Before, sorting `evil.txt` would have a massive 9,000+ stack size.
Now it has a stack size of only 30 or so.

We do this by, rather than making two recursive calls to quick sort,
we only do a recursive call on the smaller half. For the larger half,
we replace the left and right values in the current call and loop.
This way, if a poor choice of pivot is made and, for example, we
partition a block of size N into pieces 1 and N-2, we don't create
a new stack entry for N-2, but instead reuse the current one.

Fixes #62

This also fixes a unary minus on an unsigned integer, which creates
problems for some compilers (notably MSVC).
  • Loading branch information
swenson committed Sep 21, 2019
1 parent 57e04dc commit f57f8b6
Show file tree
Hide file tree
Showing 4 changed files with 45,059 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ benchmark: benchmark.c sort.h
$(CC) $(CFLAGS) benchmark.c -o benchmark

format:
astyle --options=astyle.options sort.h bitonic_sort.h demo.c multidemo.c stresstest.c benchmark.c
astyle --options=astyle.options sort.h demo.c multidemo.c stresstest.c benchmark.c
Loading

0 comments on commit f57f8b6

Please sign in to comment.