I have written about literate programming a few times before. The big idea is to elevate documentation above the actual running source code. Source code is meant for humans to read, that’s why it has higher-level abstractions and comments. Literate programming flips the roles of comments and source code. The comments come first, and the source code is stripped out and tangled into a compilable form.
I am still not totally sure what to think about it.
This post is a literate program, it both explains and implements quicksort. Quicksort is a fast, recursive sorting algorithm. The big idea is to take an array of \( n \) unsorted integers, choose a pivot element, and then recursively sort the two arrays on either side of the pivot.
Once a pivot is selected, every number to the left of the pivot is smaller, and every one to the right is bigger.
Assume your range is lower to upper, both of type int.
Choose random number in range A random integer, uniformly distributed, over that interval is generated with:
int random_number(int lower, int upper) { return (rand() % (upper - lower + 1)) + lower; } But in C, the rand() function is deterministic. That means that if you don’t “seed” the random number generator (RNG), then it will return the same sequence every time.