in-place permutation of a array follows this rule
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-place permutation of an array is an important operation in computer science where elements of an array are rearranged according to a specified permutation without using additional space for another array. This is often crucial in memory-constrained environments or when processing large datasets where the overhead of additional storage would be prohibitive.
Permutations Explained
A permutation of an array is simply a reordering of its elements. Given an array of length , a permutation is defined by a vector that specifies the new order of elements. Each element in is unique and ranges from to , representing the indices of the original array .
For example, consider an array and a permutation . By applying this permutation, the reordering of would result in .
In-Place Permutation
An in-place permutation means that we rearrange the array directly in its allocated space without using additional storage proportional to . This is achieved by modifying the array as we iterate through it, often using temporary variables for swapping and a strategy to handle cycles in permutations.
Algorithm
- Initialize: Start by iterating over each element in the array. Maintain an auxiliary visited state to keep track of which elements have been rearranged.
- Cycle Detection: For each element, check if it's part of a cycle that needs to be rearranged.
- Swap Elements In Cycle: Follow the cycle of the current element, swapping elements into their new positions.
- Mark Visited: Once a cycle is complete, mark elements as visited.
- Repeat: Continue until all elements are in their new positions.
Here's an example of code to perform in-place permutation in Python:
- Cycle Decomposition: Permutations can be decomposed into one or more cycles. Handling each cycle one at a time ensures that the permutation is correctly applied.
- Swap and Swap Back: By swapping elements directly in the array according to the permutation, space usage is minimized.
- Visited Flags: Although traditional algorithms might use extra space for visited flags, cycles can be directly resolved by reassigning indices, reducing the need for any additional marker structure.
- Memory Efficiency: In large-scale applications like image processing, where images may be represented as large matrices, in-place operations optimize memory usage.
- Shuffling Algorithms: Random shuffle algorithms in gaming and simulations often require in-place operations to ensure unbiased results without additional memory overhead.
Related reading
- In-Place Radix Sort
- In-place transposition of a matrix
- In a graph, how to calculate sum of all nodes which a node can reach efficiently?
- In a square matrix, where each cell is black or white. Design an algorithm to find the max sub-square such that all 4 borders are black
- In a FIFO Qeueing system, what''s the best way the to implement priority messaging
- In Android, how do I set margins in dp programmatically?
- In a triangulated isometric grid, what triangle is a given point in?
- Incremental median computation with max memory efficiency

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.