Cyclic Sort: Introduction

Let’s go over the Cyclic Sort pattern, its real-world applications, and some problems we can solve with it.

Overview#

Cyclic sort is an in-place, unstable, comparison sort algorithm. It is based on the insight that subsequences of numbers in the input array that are not in sorted order actually describe cycles, and that the process of placing each number in its correct position removes these cycles.

The slides below illustrate a traversal through the given array in which the value of the element at the current position determines the next position in the traversal.

canvasAnimation-image

1 of 10

canvasAnimation-image

2 of 10

canvasAnimation-image

3 of 10

canvasAnimation-image

4 of 10

canvasAnimation-image

5 of 10

canvasAnimation-image

6 of 10

canvasAnimation-image

7 of 10

canvasAnimation-image

8 of 10

canvasAnimation-image

9 of 10

canvasAnimation-image

10 of 10

As shown in the slides above, we move in a cyclic manner from the 2nd2^{nd} element onwards, returning to the same element after eight moves.

The Cyclic Sort pattern works well for problems involving arrays with numbers ranging from [1n][1 - n], where nn is the length of the array, such that one or more cycles exist in it. The algorithm places each element at its correct position within the array. This is achieved by cycling through the array and swapping each element with the element where it should be, then repeating the same operation with the swapped element, continuing in this manner until all the cycles have been removed.

Let’s look at an illustration to understand how cyclic sort is used to sort an array.

Created with Fabric.js 3.6.6 3 1 2 5 4 We have an unsorted array. Let's sort it using cyclic sort. The aim is to place elements at their correct index.correct index = current value - 1.

1 of 9

Created with Fabric.js 3.6.6 We’ve found that 3 isn’t at its correctposition, so we can swap it with the elementat index 2. 3 1 2 5 4

2 of 9

Created with Fabric.js 3.6.6 We’ve placed 3 at its correct position,but we need to check if the swapped element2 is now at its correct position. 2 1 3 5 4

3 of 9

Created with Fabric.js 3.6.6 We’ve found that 2 is not at its correct position, so we can swap it with the elementat index 1. 2 1 3 5 4

4 of 9

Created with Fabric.js 3.6.6 We’ve placed 2 at its correct position,but we need to check if the swapped element1 is now at its correct position. 1 2 3 5 4

5 of 9

Created with Fabric.js 3.6.6 1 2 3 5 4 Since 1 is already at its correct position,we can traverse the array until we finda number that isn't.

6 of 9

Created with Fabric.js 3.6.6 We’ve found that 5 is not at its correct position, so we can swap it with the elementat index 4. 1 2 3 5 4

7 of 9

Created with Fabric.js 3.6.6 1 2 3 4 5 We’ve placed 5 at its correct position,but we need to check if the swapped element4 is now at its correct position.

8 of 9

Created with Fabric.js 3.6.6 4 is also at its correct index. We now have a sorted array. 1 2 3 4 5

9 of 9

Examples#

The following examples illustrate some problems that can be solved with this approach:

Created with Fabric.js 3.6.6 index 1 2 3 4 5 6 nums 1 2 3 4 3 6 index 1 2 3 4 5 6 nums 1 3 4 3 2 6 output [3, 5] Set mismatch Given the list of numbers from 1 to n, we have to find one numberthat is repeated, and another number that is missing from the set.We solve this problem by placing the numbers at their correctindexes.

1 of 2

Created with Fabric.js 3.6.6 Find the Kth missing positive number nums 6 7 9 2 3 13 11 Given an array of numbers from 1 to n, we have to find the kthmissing number from the list. Step 2: We traverse the array and keep track of the current missing number. Whenever a number is not at its correct position in the array, we update the current missing number. When we reach the kth missing number, we return its value. k = 3 nums 13 2 3 11 9 6 7 output 5 Step 1: Apply cyclic sort to the array to correct the positions of only those elements with values between 1 and the length of the array.

2 of 2

Does my problem match this pattern?#

  • Yes, if both of these conditions are fulfilled:

    • The problem requires sorting the array without using any additional memory, and the numbers in the array are in the range [1,n][1, n], where nn is the length of the array.

    • The input array can be divided into cycles.

  • No, if any of these conditions is fulfilled:

    • The input data does not contain an array with integer values.

    • The input data is not originally in an array, nor can it be mapped to an array.

    • The values in the array are not in the [1n][1−n] range, where nn is the length of the array.

    • The problem requires stable sorting.

Real-world problems#

Many problems in the real world share the cyclic sort pattern. Let’s look at some examples.

  • Computational Biology: The species on a planet have n distinct genes numbered 1n1…n. Find the kthk^{th} missing​​ gene in a given DNA sequence.
  • Data Validation: Cyclic sort can be used for data validation tasks, especially when dealing with datasets that are expected to have distinct values within a certain range. By applying cyclic sort to the dataset, you can quickly identify any missing or duplicate values, which can help in validating the integrity and accuracy of the data.

  • Database Operations: Optimizing database operations often entails sorting arrays of distinct values. For instance, during database indexing, cyclic sort can be integrated into the sorting process, facilitating efficient organization of indexed values. This technique aids in expediting search operations and enhances overall database performance.

  • Data Analysis and Visualization: In the field of data analysis and visualization, cyclic sort can be employed to sort and organize datasets that involve distinct categorical variables. For instance, if you have a dataset containing survey responses with multiple-choice questions, cyclic sort can be used to sort and arrange the responses according to the categories or choices provided.

Strategy time!#

Match the problems that can be solved using the cyclic sort pattern.

Note: Select a problem in the left-hand column by clicking it, and then click one of the two options in the right-hand column.

Match The Answer
Select an option from the left-hand side

Given a string representing a number, return the closest number that is a palindrome.

Explanation

Cyclic Sort

Given an array of numbers in the range 11 to nn, find all the numbers that are missing in the array.

Explanation

Some other pattern

Given a set of numbers, find the first 55 missing positive numbers.

Explanation

Given a set, return the number of subsets with the sum equal to 1010.

Explanation

Missing Number