Partition an array in order
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In computer science and programming, partitioning an array in order is an operation that involves rearranging the elements of the array according to specific conditions or criteria. This concept is crucial in algorithms like quicksort, which heavily depends on partitioning. In this article, we'll delve into the various facets of this operation, exploring different methods and their applications alongside technical examples.
Understanding Array Partitioning
Partitioning an array typically involves dividing it into sections based on a condition. The simplest form of partitioning is separating elements that satisfy a certain predicate, like being lesser or greater than a pivot element. This is commonly used in sorting algorithms, where the array is reorganized such that all elements less than or equal to the pivot are on one side, and elements greater than or equal to the pivot are on the other.
Key Concepts
- Pivot Element: A value selected during partitioning which serves as the threshold for rearrangement.
- In-place Partitioning: Rearranging elements without needing extra space, crucial for efficient algorithms.
- Stability: Ensuring that the relative order of equal elements is preserved after partitioning.
Example: Lomuto's Partition Scheme
Lomuto's Partitioning is an in-place partitioning scheme often used in quicksort. Here's a step-by-step breakdown:
- Choose a Pivot: Typically the last element in the array.
- Initialize Pointers: Start with two pointers,
iwhich tracks the boundary between elements less than the pivot andjwhich scans the array. - Rearrange: For every element, compare it with the pivot. If it's smaller, swap it with the element at
iand incrementi. - Final Swap: Once all elements are checked, swap the pivot with the element at
i, placing it in its sorted position.
Benefits and Limitations
- Benefits: Easy to understand and implement, works well for average case performance.
- Limitations: Can perform poorly on already sorted arrays (degenerate case).
Hoare's Partition Scheme
Another popular method is Hoare's partition, which also rearranges elements in-place but utilizes two indices, one starting at the beginning and the other at the end of the array.
Comparative Analysis
- Efficiency: Hoare's method typically performs fewer swaps than Lomuto's in practice.
- Complexity: More complex to understand due to dual indexing.
- Stability: Both methods are not stable; they do not preserve the relative order of elements with equal keys.
Practical Applications
- Quicksort: Both Lomuto and Hoare's schemes are used to partition an array in quicksort.
- Data Segmentation: Useful in data analysis tasks where datasets need to be split based on criteria.
- Load Balancing: Arrays partitioned can help distribute loads evenly across resources.
Summary Table
| Scheme | Description | Pivot Choice | Benefits | Limitations |
| Lomuto | Single index moving in one pass | Typically last item | Simple and intuitive | Poor handling of degenerate cases |
| Hoare | Dual indices closing in | Typically first item | Fewer swaps, better for average | More complex, potential misplaced pivot in partitioning |
In conclusion, array partitioning is a fundamental component in many algorithms, especially sorting. Understanding partition schemes can significantly enhance your ability to write efficient sorting algorithms and improve overall programming skills. The right choice of partition algorithm depends on specific use cases, such as the need for stability versus performance constraints, and the nature of input data. Whether using Lomuto for simplicity or Hoare for efficiency, mastery of these techniques is essential for any programmer.
Related reading
- partitioning an float array into similar segments clustering
- Partitioning big rectangle to small ones 2D Packing
- Pass std algos predicates by reference in C
- Passing function objects into std algorithms by reference
- Pass a list to a function to act as multiple arguments
- Pass map, slice over channel and over network?
- path compression is enough for disjoint-set forests , why do we need union by rank
- Path finding Algorithms A Vs Jump Point Search

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.