## Two Short Sorts

How short can a complete, competitive sort algorithm be? Less than half a dozen lines? Maybe just 3 or 4?

How short can a complete, competitive sort algorithm be? Less than half a dozen lines? Maybe just 3 or 4?

Shuffling a collection of items is a surprisingly frequent task
in programming: essentially, it comes up whenever a known input
must be processed in random order. What is more, there is a delightful,
*three-line* algorithm to accomplish this task correctly, in-place, and
in optimal time. Unfortunately, this simple three-line solution seems
to be insufficiently known, leading to various, less-than-optimal ad-hoc
alternatives being used in practice — but that is entirely
unnecessary!

Over the holidays, I had the opportunity to work on a spare time project, and so I sat down and wrote a casual, turn-based strategy game; a variant (you might say: a clone) of the KDE game Konquest.

Finding a realistic (or at least, realistic *looking*) initial
configuration of game objects or simulation particles can be a
challenge. The desired configuration should appear to be both
“random” and at the same time “spatially uniform”, without objects
clustering together or overlapping.

I have started to get interested in Hidden Markov Models (HMM). As a warm-up, I prepared a pure Python implementation of the relevant algorithms (github).

Processing command-line arguments in ad-hoc python tools is one of those areas where I tend to just hack it together from scratch — simply because the effort of learning and understanding the relevant library packages not only seems to be more work than it is worth, but also and in particular more effort than “just doing it” by hand. I don’t want anything fancy, after all. I just want to get it done.

There are problems playing the pysolfc solitaire game on the latest release of Linux Mint 21 (Vanessa).

The game requires the `formatter`

module from the Python
Standard Library, which had been deprecated since Python 3.4,
and has been removed in Python 3.10.

An easy, but ad-hoc workaround goes as follows:

The Diamond-Square Algorithm is the natural first stop for generating artificial landscapes.
The algorithm itself is beautifully simple (more details below, and on its
Wikipedia page).
But a casual implementation ended up not working *at all*, prompting me
to look for an existing implementation to learn from. However, most
implementations I found looked hideously complicated (or just hideous),
not necessarily correct, and/or used out-of-date programming languages
and styles. It therefore seemed like a good idea to create a clean,
simple “reference” implementation of this algorithm, using a contemporary
and widely known programming language and style.

I recently came across a collection of old (1990s) “programming challenges”. I thought it might be amusing to tackle one of these challenges using technologies from the period in which they were posed, and compare the solution to one using contemporary techniques. In other words, do the same problem in C and Python.