Partition

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

Partition arranges the subarray so that elements <= Pivot are on the left and elements >= Pivot are on the right (variations differ in details). The Pivot ends up in its final position.

Lomuto and Hoare are common schemes.

Where used

Core of Quicksort and Selection (Quickselect). The same idea is behind Dutch-National-Flag partitions and in-place reorderings.

Depth

A partition rearranges a region so that values on different sides fulfill a relation to the pivot. Lomuto typically uses a boundary for the smaller region and a scan index. Hoare moves two pointers from the outside in and swaps incorrectly placed pairs.

The return value has a different meaning depending on the scheme. In Lomuto, it is often a pivot position, while in Hoare, it is usually a separator for two additional intervals. Correct recursion boundaries must therefore follow from the used scheme and must not be mixed between variants.

Difficulty levels

  1. Track the area invariants of a partition scheme.
  2. Appropriately combine return value and recursive intervals.
  3. Analyze behavior with many values equal to the pivot.

Pitfalls

Many Quicksort errors arise not in comparisons but due to inconsistent interval conventions. Inclusive and exclusive bounds must not change unnoticed within an implementation.

University approvals: 0
Tasks
Question 1

Where is the pivot after a correct partition?

Question 2

Why can't Hoare and Lomuto recursion limits be exchanged arbitrarily?

Question 3

Implement the Lomuto partition using a[hi] as the pivot and return its final index.

Hint

The pivot can be stored in a local int variable. To swap two array elements, a temporary variable is sufficient.

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.
Card Info
  • Topic: Algorithms and Data Structures
  • Difficulty: Intermediate
  • Completed: 0 users
Creator
Best
Best
BestBuddy