Cycle Sort
Cycle Sort is a comparison-based algorithm designed to minimize the total number of memory writes. It works by decomposing the array into a set of "cycles". For each element, the algorithm calculates its correct final position by counting how many elements in the array are smaller than it. It then places the element into that position, displacing the element that was previously there. This displaced element is then moved to its correct position, and the process repeats until the cycle returns to the starting point.
Demo
20 elements • 4x Speed
No Data
Did you know?
- Cycle Sort is the only in-place sorting algorithm that performs the theoretical minimum number of writes.
- Unlike most algorithms, performance doesn't degrade if the array is reverse-sorted; the "finding rank" step always checks all subsequent elements regardless of order.
- It was developed by W. D. Jones in 1963.
How it Works
-
Iterate through the array starting from
cycleStart = 0ton-2. -
Store the element at
cycleStartin a temporary variable calleditem. -
Find Rank: Scan all elements to the right of
cycleStart. Count how many elements are smaller thanitem. This count added tocycleStartgives the correctpos(position) foritem. -
Skip Duplicates: If
itemis equal to the element currently atpos, incrementposto placeitemafter the duplicates. -
Write: If
posis different fromcycleStart, writeitemtoarr[pos]. The value originally atarr[pos]becomes the newitemto be placed. -
Rotate Cycle: Repeat the find-rank and write process for the new
itemuntilposequalscycleStart. -
Move to the next
cycleStartand repeat.
Complexity Analysis
Advantages
- Optimal Writes: It performs the minimum number of memory writes theoretically possible to sort an array ( minus the number of cycles).
- In-Place: Requires constant auxiliary memory.
- Useful for EEPROM: Because it minimizes writes, it is suitable for memory types where write operations significantly reduce the lifespan of the hardware (like Flash memory).
Disadvantages
- Slow Time Complexity: It runs in time in all cases (Best, Average, and Worst), making it significantly slower than algorithms for large datasets.
- Unstable: It does not preserve the relative order of equal elements.
- Complex Logic: More complex to implement correctly compared to other algorithms like Bubble or Insertion Sort.
Implementation
function cycleSort(arr) {
const n = arr.length;
// Traverse array elements and put them in the right place
for (let cycleStart = 0; cycleStart <= n - 2; cycleStart++) {
let item = arr[cycleStart];
// Find position where we put the item
let pos = cycleStart;
for (let i = cycleStart + 1; i < n; i++) {
if (arr[i] < item) {
pos++;
}
}
// If item is already in correct position
if (pos === cycleStart) {
continue;
}
// Ignore all duplicate elements
while (item === arr[pos]) {
pos += 1;
}
// Put the item to its right position
if (pos !== cycleStart) {
let temp = item;
item = arr[pos];
arr[pos] = temp;
}
// Rotate rest of the cycle
while (pos !== cycleStart) {
pos = cycleStart;
// Find position where we put the element
for (let i = cycleStart + 1; i < n; i++) {
if (arr[i] < item) {
pos += 1;
}
}
// Ignore all duplicate elements
while (item === arr[pos]) {
pos += 1;
}
// Put the item to its right position
if (item !== arr[pos]) {
let temp = item;
item = arr[pos];
arr[pos] = temp;
}
}
}
return arr;
}