My Quickshort interview with Sir Tony Hoare, the inventor of Quicksort

If you studied Computer Science, at some point, usually in the beginning of your algorithm courses, you learn about sorting algorithms, the most famous one being Quicksort. Studies show that the most frequent reactions to those who learn about Quicksort for the very first time are:

1) Wow!!! How could anyone possibly come up with that idea?
2) Hmm, explain it to me one more time please?
3) #1 followed by #2, or #2 follower by #1

Not surprisingly I was right there in the #3 category. To learn about Quicksort just Bing it, but this blog post isn't about Quicksort necessarily, but rather about the person who invented it, Sir Tony Hoare. The winner of the Turing Award prize in 1980, Sir Tony Hoare currently works at Microsoft, just like me, and he was kind enough to give me the honor to interview him for my coding blog. Without any further due, here it is - I hope you Quick-enjoy it too! :)

[Marcelo De Barros] First, thank you Tony for the incredible opportunity to ask you few questions! Let's start with the Quicksort: what’s your recollection of the day when you came up with the idea of the Quicksort? Can you walk us through how you came up with that idea?

[Sir Tony Hoare] I remember it clearly. In 1960, I was lounging on my bed/settee in my room in Moscow State University, thinking how I would write a program (in Mercury Autocode, the only programming language I knew) to sort words of a Russian sentence prior to looking them up in a Russian-English dictionary, which was held in alphabetic order on magnetic tape. Thus all the relevant entries for the whole sentence could be pulled off the tape in a single scan.
The first Idea I had was insertion sort, but I recognized that this a bit slow (quadratic). My second idea was quicksort. It occurred to me just as quickly as the first idea. I wrote the program for the partition, but I couldn’t write the program to account for the list of unsorted segments.

On return to England, I got a job with a small computer manufacturer. My first task was to write program the Shellsort algorithm, an in-situ sort that had recently been published. On completion of this task, I timidly told my boss that I thought I knew of a faster method. He bet me sixpence that I did not. He soon agreed that he had lost his bet.
Later, I discovered recursion from my reading of the Report on the ALGOL language. This enabled me to publish the program in algorithms section of the ACM


[Marcelo De Barros] So, did you manage to learn Russian after all? :)

[Sir Tony Hoare] I learnt Russian full-time on National Service in the Royal Navy, and qualified as an Interpreter.

[Marcelo De Barros] Did you know at the time that Quicksort was going to be a major breakthrough in Computer Science?

[Sir Tony Hoare] I knew that my algorithm was n log n, same as Mergesort, but better because it could sort twice the number of items in main memory. The innermost loop was also fast. I later did some math that showed and exact formula for the average running time, and published a paper in the British computer Journal.
Since I did not have any postgraduate degree, it was very lucky that I discovered quicksort. I gather now that another algorithm (Timsort?) is faster when sorting vast lists that are already nearly sorted.


[Marcelo De Barros] What is it like to work for Microsoft? :)

[Sir Tony Hoare] I’ve had a great fifteen years. Recently I have discovered the basic mathematical laws for Concurrent Programming, and achieved a unification of many previous theories about it.

[Marcelo De Barros] You know, this is a coding blog and as such I must write some code towards the end of every post, but this time around I'll pass the token to you to write some code - any code! :) Would you please?

[Sir Tony Hoare] I code in mathematics, so my snippet is a fundamental algebraic law which governs the interaction between concurrency || and sequentially (;) in computer programs:
(p||q) ; (p’||q’) < (p;p’) || (q;q’)

It’s not a new law, but it is ubiquitous and pervasive.

Thank you very much, Tony! And thanks to all the readers too - I'll see you next time!

Cheers,
Marcelo

Comments

  1. Congratulations Marcelo with interviewing a real legend! Considering significant percentage of CPU cycles spent on sorting, it's not an exaggeration to say that Sir Tony Hoare not only inspired generations of computer scientists (including me), but also made an outstanding contribution in saving nature by reducing amount of resources powering our busy CPUs.

    I remember back in university I was reading about Hoare logic and feeling for the first time that programming is an actual science with formal methods to reason about the code, and not just writing a random set of characters in a hope that it produces a desired output. It takes a touch of a genius to express profound ideas from parallel and distributed (which is fundamentally pretty much the same as parallel) programming in such a concise and elegant expression as: (p||q) ; (p’||q’) < (p;p’) || (q;q’)

    It's true that some of the popular libraries and frameworks switched to Timsort, but it's implementation is trickier, which most likely contributed to introduction of only recently discovered bug in its Android Java and Python implementation (http://envisage-project.eu/proving-android-java-and-python-sorting-algorithm-is-broken-and-how-to-fix-it). Personally I find QuickSort much more elegant and, well, quick in practice, so only after a careful performance benchmarks on common data patterns I work with, would I consider to move to any other algorithm for sorting.

    Thank you very much Sir Tony for your hard and inspiring work!

    Well, Marcelo, now that you set the bar so high, I hope some day you'll also interview another legend and superhero - Leslie Lamport.

    Thank you and have a marvelous rest of the weekend!

    Reply Delete

Post a Comment

[フレーム]

Popular posts from this blog

Quasi FSM (Finite State Machine) problem + Vibe

Not really an FSM problem since the state isn't changing, it is just defined by the current input. Simply following the instructions should do it. Using VSCode IDE you can also engage the help of Cline or Copilot for a combo of coding and vibe coding, see below screenshot. Cheers, ACC. Process String with Special Operations I - LeetCode You are given a string  s  consisting of lowercase English letters and the special characters:  * ,  # , and  % . Build a new string  result  by processing  s  according to the following rules from left to right: If the letter is a  lowercase  English letter append it to  result . A  '*'   removes  the last character from  result , if it exists. A  '#'   duplicates  the current  result  and  appends  it to itself. A  '%'   reverses  the current  result . Return the final string  result  after processing all char...

Shortest Bridge – A BFS Story (with a Twist)

Here's another one from the Google 30 Days challenge on LeetCode — 934. Shortest Bridge . The goal? Given a 2D binary grid where two islands (groups of 1s) are separated by water (0s), flip the fewest number of 0s to 1s to connect them. Easy to describe. Sneaky to implement well. 🧭 My Approach My solution follows a two-phase Breadth-First Search (BFS) strategy: Find and mark one island : I start by scanning the grid until I find the first 1 , then use BFS to mark all connected land cells as 2 . I store their positions for later use. Bridge-building BFS : For each cell in the marked island, I run a BFS looking for the second island. Each BFS stops as soon as it hits a cell with value 1 . The minimum distance across all these searches gives the shortest bridge. πŸ” Code Snippet Here's the core logic simplified: public int ShortestBridge(int[][] grid) { // 1. Mark one island as '2' and gather its coordinates List<int> island = FindAndMark...

Classic Dynamic Programming IX

A bit of vibe code together with OpenAI O3. I asked O3 to just generate the sieve due to laziness. Sieve is used to calculate the first M primes (when I was using Miller-Rabin, was giving me TLE). The DP follows from that in a straightforward way: calculate the numbers from i..n-1, then n follows by calculating the min over all M primes. Notice that I made use of Goldbach's Conjecture as a way to optimize the code too. Goldbach's Conjecture estates that any even number greater than 2 is the sum of 2 primes. The conjecture is applied in the highlighted line. Cheers, ACC. PS: the prompt for the sieve was the following, again using Open AI O3 Advanced Reasoning: " give me a sieve to find the first M prime numbers in C#. The code should produce a List<int> with the first M primes " Minimum Number of Primes to Sum to Target - LeetCode You are given two integers  n  and  m . You have to select a multiset of  prime numbers  from the  first   m  pri...