Boolean arrays
reordering algorithms
in-place algorithms
time complexity
space complexity

Boolean array reordering in O1 space and On time

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If an array contains only boolean values, reordering it so that all False values come before all True values is just a two-value partitioning problem. Because there are only two possible values, you can solve it in linear time and constant extra space with a simple two-pointer technique.

The Core Idea

You want the final array to look like this:

text
[False, False, ..., True, True]

Since every element is either False or True, you do not need a general-purpose sorting algorithm. A partition algorithm is enough.

The usual in-place strategy keeps:

  • one pointer moving from the left toward the right
  • one pointer moving from the right toward the left

Whenever the left side has a True that should move right, and the right side has a False that should move left, swap them.

Two-Pointer Solution

Here is a Python implementation.

python
1def reorder_booleans(values):
2    left = 0
3    right = len(values) - 1
4
5    while left < right:
6        while left < right and values[left] is False:
7            left += 1
8
9        while left < right and values[right] is True:
10            right -= 1
11
12        if left < right:
13            values[left], values[right] = values[right], values[left]
14            left += 1
15            right -= 1
16
17
18arr = [True, False, True, False, False, True]
19reorder_booleans(arr)
20print(arr)

This runs in O(n) time because each pointer moves across the array at most once.

Why The Space Complexity Is O(1)

The algorithm uses only a few variables:

  • 'left'
  • 'right'
  • temporary swap storage managed by Python assignment

No extra array or hash structure is created, so the auxiliary space stays constant.

A Slightly Simpler One-Pass Variant

You can also solve this with a write pointer that tracks the next position where False should go.

python
1def reorder_booleans_single_pass(values):
2    write = 0
3
4    for read in range(len(values)):
5        if values[read] is False:
6            values[write], values[read] = values[read], values[write]
7            write += 1
8
9
10arr = [True, False, True, False, False, True]
11reorder_booleans_single_pass(arr)
12print(arr)

This is still O(n) time and O(1) extra space. It is often easier to read than the two-ended version.

Stability Matters

If the problem only asks for all False values before all True values, the methods above are perfect.

But they are not stable in the general algorithmic sense. That means if each boolean were attached to additional payload data and you cared about preserving relative order within the False or True groups, a simple in-place swap partition might not preserve that order.

For plain boolean arrays, stability usually does not matter because equal values are indistinguishable.

Why This Is Better Than Sorting

A general sort would take more machinery than necessary. Even if the implementation is fast in practice, the algorithmic idea is overkill.

This problem has only two categories, so the right mental model is partition, not comparison sorting.

That is why the linear-time in-place solution is both simpler and more efficient.

Edge Cases

A good implementation should already handle:

  • empty arrays
  • arrays of all False
  • arrays of all True
  • arrays of length 1

The loop conditions above work correctly on all of these without special case code.

Common Pitfalls

A common mistake is using a full sorting function and then calling it optimal because booleans are easy to compare. That ignores the fact that the problem has a simpler partition-based solution.

Another issue is forgetting what O(1) space means. Creating a second array to hold reordered elements breaks the constant-space requirement, even if the code is easy to write.

Developers also sometimes overcomplicate the boolean checks. Since the array contains only two values, the logic should stay simple and explicit.

Finally, if your input is not actually guaranteed to be boolean, validate that assumption first. These partition methods rely on there being only two categories.

Summary

  • Reordering a boolean array is a partition problem, not a general sorting problem.
  • You can do it in O(n) time and O(1) extra space.
  • A two-pointer or single-pass write-pointer method both work well.
  • These solutions are ideal when the only goal is to group all False values before all True values.
  • If stability or additional payload ordering matters, be explicit about that requirement before choosing the in-place method.

Course illustration
Course illustration

All Rights Reserved.