literate-programming posts

Literate Quicksort

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.

Generating random numbers in C

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.