Skip to content

*Cyclic Sort

Code with Senpai edited this page Oct 7, 2020 · 1 revision
def cyclic_sort(nums):
  i = 0
  while i < len(nums):
    j = nums[i] - 1
    if nums[i] != nums[j]:
      nums[i], nums[j] = nums[j], nums[i]  # swap
    else:
      i += 1
  return nums

Clone this wiki locally