Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
Chaturmind
← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
HomeLearnDSADSA Patterns for InterviewsCoding Patterns
✓ FreeIntermediate· 11 min read

Cyclic Sort

Use cyclic sort to find missing numbers, duplicates, and corrupted elements in O(n) time and O(1) space.

Published March 26, 2025


Cyclic Sort

Cyclic sort is a special sorting technique for arrays where elements are in a known range [1, n]. It sorts in O(n) time and O(1) space by placing each number at its correct index.

The Pattern

For an array with values 1 to n, the correct position for value v is index v-1. Cycle through the array, swapping each element to its correct position.

void cyclicSort(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        int correctIdx = nums[i] - 1; // where nums[i] should be
        if (nums[i] != nums[correctIdx]) {
            // Swap nums[i] to its correct position
            int tmp = nums[i];
            nums[i] = nums[correctIdx];
            nums[correctIdx] = tmp;
        } else {
            i++; // nums[i] is at its correct position
        }
    }
}
// [3,1,5,4,2] → sort → [1,2,3,4,5]

Find Missing Number

public int missingNumber(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        int j = nums[i];
        if (j < nums.length && nums[i] != nums[j]) {
            int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
        } else {
            i++;
        }
    }
    // Find the first position where nums[i] != i
    for (int k = 0; k < nums.length; k++)
        if (nums[k] != k) return k;
    return nums.length; // missing number is n
}
// [3,0,1] → sort → [0,1,3] → position 2 is wrong → missing = 2

Find All Missing Numbers

public List<Integer> findDisappearedNumbers(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        int j = nums[i] - 1;
        if (nums[i] != nums[j]) {
            int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
        } else {
            i++;
        }
    }
    List<Integer> missing = new ArrayList<>();
    for (int k = 0; k < nums.length; k++)
        if (nums[k] != k + 1) missing.add(k + 1);
    return missing;
}

Find Duplicate Number

public int findDuplicate(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        if (nums[i] != i + 1) {
            int j = nums[i] - 1;
            if (nums[i] != nums[j]) {
                int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
            } else {
                return nums[i]; // duplicate found!
            }
        } else {
            i++;
        }
    }
    return -1;
}

Find All Duplicates

public List<Integer> findAllDuplicates(int[] nums) {
    int i = 0;
    while (i < nums.length) {
        int j = nums[i] - 1;
        if (nums[i] != nums[j]) {
            int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
        } else {
            i++;
        }
    }
    List<Integer> duplicates = new ArrayList<>();
    for (int k = 0; k < nums.length; k++)
        if (nums[k] != k + 1) duplicates.add(nums[k]);
    return duplicates;
}

When to Apply Cyclic Sort

✅ Array contains numbers in range [1, n] or [0, n-1] ✅ Problem asks for missing/duplicate/corrupted numbers ✅ O(1) space required

Interview Tips

  1. Cyclic sort is O(n) time — each element is moved at most once to its correct position, so total swaps ≤ n.
  2. The key check: nums[i] != nums[j] before swapping prevents infinite loops with duplicates.
  3. After sorting, scan for nums[i] != i+1 to identify missing or duplicate values.

Previous

Merge Intervals

Next

Top-K Elements

AI Tutor

Lesson: Cyclic Sort

Quick actions

AI responses can be inaccurate. Verify critical information.