Coding: Sort an Array using Cyclic Sort Algorithm in JavaScript

Monu Kumar Modi
JavaScript in Plain English
3 min readApr 26, 2023

--

Cyclic Sort is an algorithm to sort an array of integers in the range of 1 to n (inclusive) using the property that each integer in the array is unique.

The Cyclic Sort algorithm is an in-place sorting algorithm that can be used to sort arrays containing unique integers from 1 to n. The algorithm works by iterating through the array and placing each element in its correct position by swapping it with the element at the correct position. The algorithm repeats this process until all the elements are in their correct positions.

The Cyclic Sort algorithm is a very efficient algorithm because it has a time complexity of O(n), which means it can sort an array of n elements in linear time. Additionally, the algorithm does not require any extra space, making it a very memory-efficient algorithm.

To sort an array using the Cyclic Sort algorithm in JavaScript, we can define a function that takes an array as an argument and performs the sorting operation on the array. Here’s an example of how to implement the Cyclic Sort algorithm in JavaScript:

Cyclic Sort Array

In this function, we first initialize two variables, i and n, to 0 and the length of the array, respectively. We then enter a while loop that continues until i is less than n. Inside the while loop, we use a constant variable j to represent the index of the element that should be at the current index i. We check if the element at index i is not equal to the element at index j. If it is not equal, we swap the two elements using ES6 destructuring assignment. If the elements are equal, we increment i and continue the loop.

To test our function, we can define an array and call the function with that array. Here’s an example:

In this example, we define an array containing six elements and call the cyclicSort function with that array. We then log the sorted array to the console.

Complete Code:

// Sort an Array using cyclic sort (1-n)
function cyclicSort(nums) {
let i = 0
let n = nums.length
while (i < n) {
const j = nums[i] - 1
if (nums[i] !== nums[j]) {
;[nums[i], nums[j]] = [nums[j], nums[i]]
} else {
i++
}
}
return nums
}
const arr = [1, 2, 4, 5, 6, 3]
const sortedArray = cyclicSort(arr)
console.log(sortedArray)

Conclusion

Sorting arrays is a common task in programming, and there are several algorithms that can be used to accomplish this task. The Cyclic Sort algorithm is an efficient in-place sorting algorithm that can be used to sort arrays containing unique integers from 1 to n. In this blog post, we discussed how to use the Cyclic Sort algorithm to sort an array in JavaScript. We hope that you found this post helpful and informative!

If you’re interested in learning more about coding in Javascript, be sure to follow me for more code snippets and examples. I’ll be sharing valuable information and tips on how to master the language and improve your skills as a developer. So, stay tuned for more updates, and let’s continue learning together.

Thanks for reading.Happy learning 😄

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

--

--