Cyclic Sort (easy)

Problem Statement #

We are given an array containing ‘n’ objects. Each object, when created, was assigned a unique number from 1 to ‘n’ based on their creation sequence. This means that the object with sequence number ‘3’ was created just before the object with sequence number ‘4’.

Write a function to sort the objects in-place on their creation sequence number in O(n)O(n) and without any extra space. For simplicity, let’s assume we are passed an integer array containing only the sequence numbers, though each number is actually an object.

Example 1:

Input: [3, 1, 5, 4, 2]
Output: [1, 2, 3, 4, 5]

Example 2:

Input: [2, 6, 4, 3, 1, 5]
Output: [1, 2, 3, 4, 5, 6]

Example 3:

Input: [1, 5, 6, 4, 3, 2]
Output: [1, 2, 3, 4, 5, 6]

Try it yourself #

Try solving this question here:

0 of 3 Tests Passed
ResultInputExpected OutputActual OutputReason
cyclic_sort([3, 1, 5, 4, 2])[1, 2, 3, 4, 5])[3, 1, 5, 4, 2]Incorrect Output
cyclic_sort([2, 6, 4, 3, 1, 5])[1, 2, 3, 4, 5, 6])[2, 6, 4, 3, 1, 5]Incorrect Output
cyclic_sort([1, 5, 6, 4, 3, 2])[1, 2, 3, 4, 5, 6])[1, 5, 6, 4, 3, 2]Incorrect Output
0.488s

Solution #

As we know, the input array contains numbers in the range of 1 to ‘n’. We can use this fact to devise an efficient way to sort the numbers. Since all numbers are unique, we can try placing each number at its correct place, i.e., placing ‘1’ at index ‘0’, placing ‘2’ at index ‘1’, and so on.

To place a number (or an object in general) at its correct index, we first need to find that number. If we first find a number and then place it at its correct place, it will take us O(N2)O(N^2), which is not acceptable.

Instead, what if we iterate the array one number at a time, and if the current number we are iterating is not at the correct index, we swap it with the number at its correct index. This way we will go through all numbers and place them in their correct indices, hence, sorting the whole array.

Let’s see this visually with the above-mentioned Example-2:

Created with Fabric.js 1.6.0-rc.1 After the swap, number '2' is placed at its correct index. Number '2' is not at its correct place, let's swap it with the correct index. 2 6 4 3 1 5 2 6 4 3 1 5 6 2 4 3 1 5 1 2 3 4 5 6 Whole array is sorted. 6 2 4 3 1 5 Number '6' is not at its correct place, let's swap it with the correct index. We'll not move on to the next number until we have a correct number at 'start'. 5 2 4 3 1 6 After the swap, number '6' is placed at its correct index. We'll not move on to the next number until we have a correct number at 'start'. Number '5' is not at its correct place, let's swap it with the correct index. 1 2 4 3 5 6 After the swap, both '6' and '1' are placed at their correct place. Number '1' is at its correct index, let's move on to the next number. 1 2 4 3 5 6 1 2 4 3 5 6 Number '2' is at its correct index, let's move on to the next number. 1 2 3 4 5 6 After the swap, both '3' and '4' are placed at their correct place. All the remaining numbers are at their correct places. Number '4' is not at its correct place, let's swap it with its correct index.

Code #

Here is what our algorithm will look like:

Output

0.640s

[1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]

Time complexity #

The time complexity of the above algorithm is O(n)O(n). Although we are not incrementing the index i when swapping the numbers, this will result in more than ‘n’ iterations of the loop, but in the worst-case scenario, the while loop will swap a total of ‘n-1’ numbers and once a number is at its correct index, we will move on to the next number by incrementing i. So overall, our algorithm will take O(n)+O(n1)O(n) + O(n-1) which is asymptotically equivalent to O(n)O(n).

Space complexity #

The algorithm runs in constant space O(1)O(1).

Mark as Completed
←    Back
Introduction
Next    →
Find the Missing Number (easy)