Memorizing computer science algorithms

I was trying to use the techniques of: a character, narrative, and emotion. In my head, it’s like a comic strip with some of the frames animated. What I’m trying to memorize has nothing to do with the Enigma machine, it’s just that Allan Turing was a computer scientist who appeared in that movie “Imitation Game” which I saw. So that provides a nice memory hook to something known.

That’s a good point. Let me try:

  • Quicksort takes an array and sorts it “in-place” without making any copies of data. It does this in three basic steps:
    1. Select a pivot element p
    2. Partition the array A around p such that A_first_half < p < A_last_half
    3. Recursively sort array halves “first” and “last” using quicksort

That’s the basic algorithm called quicksort. I was wondering out loud if I should memorize the basic algorithm (i.e. the three steps above) or if I should memorize a specific implementation in a programming language. I opted for the latter.

I’ve connected the story bullet point numbers to the code below

implementation with mnemonics as comments
def qsort(L, first, last):
    if first >= last:  # story line 2
        return

    p = L[random.randint(first, last)]  # story line 3

    i, j = first, last # story line 5
    while i < j:
        while L[i] < p:  # story line 4, 6, 7
            i += 1
        while L[j] > p:  # story line 4, 6, 8
            j -= 1
        L[i], L[j] = L[j], L[i]  # story line 9
        # story line 10
        if L[i] != p:  # story line 11
            i += 1 
        else:
            j -= 1  # story line 11
    # story line 12
    qsort(L, first, i - 1)  # story line 13
    qsort(L, j + 1, last)  # story line 13