Project Euler Lab - Problem 523

#523 - First Sort I

● AppliedOfficial difficulty: 20%RandomnessTier B - browser, with the efficient algorithmNot viewed
↖ Euler Lab

Consider the following algorithm for sorting a list:

  • 1. Starting from the beginning of the list, check each pair of adjacent elements in turn.
  • 2. If the elements are out of order:
    • a. Move the smallest element of the pair at the beginning of the list.
    • b. Restart the process from step 1.
  • 3. If all pairs are in order, stop.

For example, the list \(\{\,4\,1\,3\,2\,\}\) is sorted as follows:

  • \(\underline{4\,1}\,3\,2\) (\(4\) and \(1\) are out of order so move \(1\) to the front of the list)
  • \(1\,\underline{4\,3}\,2\) (\(4\) and \(3\) are out of order so move \(3\) to the front of the list)
  • \(\underline{3\,1}\,4\,2\) (\(3\) and \(1\) are out of order so move \(1\) to the front of the list)
  • \(1\,3\,\underline{4\,2}\) (\(4\) and \(2\) are out of order so move \(2\) to the front of the list)
  • \(\underline{2\,1}\,3\,4\) (\(2\) and \(1\) are out of order so move \(1\) to the front of the list)
  • \(1\,2\,3\,4\) (The list is now sorted)

Let \(F(L)\) be the number of times step 2a is executed to sort list \(L\). For example, \(F(\{\,4\,1\,3\,2\,\}) = 5\).

Let \(E(n)\) be the expected value of \(F(P)\) over all permutations \(P\) of the integers \(\{1, 2, \dots, n\}\).
You are given \(E(4) = 3.25\) and \(E(10) = 115.725\).

Find \(E(30)\). Give your answer rounded to two digits after the decimal point.

This problem is taken from Project Euler, Problem 523.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=523. Published Sunday, 6th September 2015, 07:00 am. Solved by 1,212 members at time of mirroring.

Why this is useful

Probability Statistics. Expectation and state-based probability reasoning underpin pricing, risk, and statistical inference (Phases 7, 11, 13).

We classify relevance honestly - not every Euler problem is a trading application.

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier B - browser, with the efficient algorithm
Runs in the browser only with the intended efficient algorithm; a naive loop will hit the timeout.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.