Implementation: Hoare/Lomuto-light Partition

Intermediate Algorithms and Data Structures English
Also available: Deutsch
Created by Best · 16.08.2026 at 09:13 UTC

Implement a simple Lomuto partition using the last element as the pivot.

Variant Return value
Lomuto index of the pivot
Hoare meeting point of the pointers
## Where used

Anyone writing a partition understands Quicksort bugs and off-by-one errors at the boundaries. It is useful when reading JDK and teaching code.

Depth

A Lomuto-light variant can choose the last element as the pivot. Before the scan, only values that correspond to the chosen relation are on the left of the boundary; between the boundary and the scan, already examined values of the other group lie. After the scan, the pivot is set at the boundary.

A Hoare-close variant uses two indices and swaps pairs that are on the wrong side. It often requires fewer swaps, but its return value defines a boundary. Both variants are correct when invariants, comparison operators, and recursive intervals match.

Difficulty levels

  1. Check a Lomuto invariant during each scan step.
  2. Ensure safe progress for Hoare pointers with duplicates.
  3. Specify correct recursive sub-intervals for each schema.

Pitfalls

Mixed rules are dangerous, such as a Hoare return value with Lomuto boundaries. When there are the same pivot values, pointers must still make progress to ensure the loop terminates.

University approvals: 0
Tasks
Question 1

Implement partition(a, lo, hi) with pivot a[hi]; return the final pivot index.

Hint

Array access is done with a[index]. A swap can be formulated using a local int variable.

Starter code is prefilled; replace TODO blocks with your solution.
1 test case will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Question 2

What must be ensured in a Hoare-close loop with many pivot duplicates?

Card Info
  • Topic: Algorithms and Data Structures
  • Difficulty: Intermediate
  • Completed: 0 users
Creator
Best
Best
BestBuddy