Cyclic Sort

Cyclic Sort is an in-place sorting algorithm that works particularly well when you have an array of distinct integers in a specific range. It operates by iteratively placing each element in its correct position within the range.

Cyclic Sort works well when you have distinct integers within a known range, as each element corresponds directly to its desired index in the sorted array. The algorithm operates with a time complexity of O(n), making it efficient for smaller arrays.

It’s important to note that Cyclic Sort is not suitable for arrays with duplicate elements or for ranges that don’t start at 0. Additionally, the algorithm modifies the input array in-place, so it’s not stable (i.e., the relative order of equal elements might change).

  • Kth Missing Positive Number
    Given an array arr of positive integers sorted in ascending order, and an integer k, find the kth positive integer that is missing from the array.
  • First Missing Positive
    Given an unsorted integer array nums, find the smallest missing positive integer.
  • Find All Duplicates in an Array
    Given an array nums of n integers where each integer is in the range [1, n], return an array of all the integers that appear more than once in the array, sorted in ascending order.
  • Find All Numbers Disappeared in an Array
    Given an array nums of n integers where each integer is in the range [1, n], return an array of all the integers that do not appear in the array, sorted in ascending order.
  • Find the Duplicate Number
    Given an array nums containing n + 1 integers where each integer is in the range [1, n], prove that there is only one duplicate number in the array. Return this duplicate number.
  • Set Mismatch
    You have a set of integers nums that contains integers from 1 to n. However, there is one duplicate number and one missing number in the set. Return an array containing the duplicate number and the missing number.
  • Missing Number
    You are given an array nums containing n distinct numbers taken from the range [0,…