In-Place Algorithms

What Is an In-Place Algorithm?

An in-place algorithm works on its input directly and uses only a small, fixed amount of extra memory. Learn the definition, examples, and trade-offs.

On this page
  1. What “in-place” means
  2. A simple example: reversing an array
  3. Common in-place algorithms
  4. Algorithms that are usually not in-place
  5. Why the distinction matters
  6. Trade-offs
  7. Space is not the same as time
  8. How to tell whether an algorithm is in-place
  9. Key takeaways

An in-place algorithm works directly on its input and uses only a small, fixed amount of extra memory. Instead of building a full copy of the data, it rearranges or updates the values where they already sit. The input is usually overwritten by the output. The term comes up most often in sorting and array processing.

What “in-place” means

The key question is how much memory the algorithm needs beyond the data it receives. An in-place algorithm may use a few extra variables, such as loop counters, index positions, or a temporary value during a swap. That extra space should not grow with the size of the input.

Sources differ on the exact limit. The strictest definition allows only a constant amount of extra space, written O(1). A looser definition, which many texts use, also accepts O(log n) extra space. That allowance covers the index needed to point into an array of n items, along with the small call stack some recursive algorithms use. Under either definition, the algorithm does not create a new structure as large as its input.

A simple example: reversing an array

Suppose you want to reverse the list 1, 2, 3, 4, 5. An in-place approach places one position marker at the start and another at the end. It swaps the two values, moves the markers one step toward the middle, and repeats until they meet. The result is 5, 4, 3, 2, 1. The only extra memory is the two markers and one temporary value for each swap.

A version that is not in-place would create a second list, fill it from the back, and then use that list as the result. That works, but it needs a second list as long as the original.

Common in-place algorithms

Arrays are the usual setting for this kind of work, because their values sit side by side and can be swapped where they stand. Several classic methods work this way:

  • Insertion sort, selection sort, and bubble sort rearrange items inside the array by swapping or shifting them, using only a few extra variables.
  • Heapsort arranges the array into a heap and then repeatedly moves the largest remaining value to the end. Because the heap lives inside the array, the method is in-place. The heapsort algorithm shows this step by step.
  • Quicksort partitions the array by swapping values around a pivot, and that partitioning is in-place. Its recursive calls use stack space, which stays near O(log n) when the implementation always recurses into the smaller side first. Without that step, the depth can grow to O(n) in the worst case.
  • The Fisher-Yates shuffle randomly reorders an array by swapping each position with a random position at or before it. It needs no extra array.

Algorithms that are usually not in-place

Not every efficient algorithm is in-place. A standard merge sort for arrays copies values into a temporary buffer while merging, so it needs extra space that grows with the input. In-place merge sort variants exist, but they are more involved to write. Counting sort typically needs an array of counts and an output array, so it is usually not in-place either.

Why the distinction matters

Extra memory can be a real constraint:

  • Devices with tight memory, such as small embedded systems, may not have room for a second copy of the data.
  • Very large inputs can make copying expensive, or impossible to hold in memory at once.
  • Fewer copies usually mean less data movement, which can help performance, though it does not guarantee faster code.

Trade-offs

In-place methods come with costs:

  • The original data changes. If you need the unsorted input later, keep a copy before you start.
  • Stability may be lost. A stable sort keeps equal items in their original order. Heapsort is not stable, and the usual quicksort is not either. Which sorting algorithms are stable is a separate question from whether they are in-place.
  • The code can be harder to follow. Index arithmetic and careful swaps are easy to get wrong, and off-by-one errors are common.

Space is not the same as time

An in-place algorithm says nothing about how fast it runs. Insertion sort is in-place, yet its worst case takes O(n²) time. Heapsort is also in-place, and its worst case is O(n log n). Time and space are analyzed separately, usually with Big-O notation. For a deeper look at that notation, see big o data structures.

How to tell whether an algorithm is in-place

Ask a few questions:

  1. Does it create a new array or list about as long as the input? If so, it is probably not in-place.
  2. Does it write its results back into the original storage?
  3. Does the number of extra variables stay fixed as the input grows?
  4. If it uses recursion, how deep can the call stack get for the worst input?

A yes to the first question, or deep recursion on large inputs, means the algorithm uses more memory than the in-place label suggests.

Key takeaways

  • An in-place algorithm uses only a small, fixed amount of extra memory, and it usually overwrites its input with the output.
  • Reversing an array by swapping its ends is a simple example.
  • Insertion sort, heapsort, and the usual quicksort are common in-place examples. Standard merge sort is not.
  • In-place says nothing about speed. It can also mean losing stability or the original data.

Keep exploring

Guides from other topics

All topics →

Big O for Data Structures

A practical guide to Big O for data structures: how arrays, lists, hash tables, trees and heaps compare on search, insert and delete costs, with examples.

Big O Notation 5 min read