Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/algorithms/sorting/cycle-sort/CycleSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Sort from '../Sort';

export default class CycleSort extends Sort {
sort(originalArray) {
const array = [...originalArray];
const length = array.length;

for (let cycleStart = 0; cycleStart < length - 1; cycleStart += 1) {
let item = array[cycleStart];
let position = cycleStart;

// Call visiting callback.
this.callbacks.visitingCallback(array[cycleStart]);

// Find correct position for the current item.
for (let i = cycleStart + 1; i < length; i += 1) {
this.callbacks.visitingCallback(array[i]);

if (this.comparator.lessThan(array[i], item)) {
position += 1;
}
}

// If item is already in correct position.
if (position === cycleStart) {
continue;
}

// Skip duplicates.
while (
position < length &&
this.comparator.equal(item, array[position])
) {
position += 1;
}

// Swap.
if (position !== cycleStart) {
[array[position], item] = [item, array[position]];
}

// Rotate the rest of the cycle.
while (position !== cycleStart) {
position = cycleStart;

for (let i = cycleStart + 1; i < length; i += 1) {
this.callbacks.visitingCallback(array[i]);

if (this.comparator.lessThan(array[i], item)) {
position += 1;
}
}

while (
position < length &&
this.comparator.equal(item, array[position])
) {
position += 1;
}

if (!this.comparator.equal(item, array[position])) {
[array[position], item] = [item, array[position]];
}
}
}

return array;
}
}
28 changes: 28 additions & 0 deletions src/algorithms/sorting/cycle-sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Cycle Sort

Cycle sort is an in-place, unstable sorting algorithm that minimizes the number of writes to the array. It is theoretically optimal in terms of total number of memory writes.

![Cycle Sort Visualization](https://upload.wikimedia.org/wikipedia/commons/5/5e/Cycle_sort_animation.gif)

## How It Works

Cycle Sort works by determining the correct position of each element and rotating elements in cycles until each element is placed in its correct position.

Unlike traditional sorting algorithms, Cycle Sort is optimal when memory writes are expensive.

## Complexity

| Name | Best | Average | Worst | Memory | Stable |
|------------|--------|---------|--------|--------|--------|
| Cycle Sort | O(n²) | O(n²) | O(n²) | O(1) | No |

## Characteristics

- In-place sorting
- Minimizes memory writes
- Not stable
- Useful when write operations are costly

## References

- https://en.wikipedia.org/wiki/Cycle_sort
22 changes: 22 additions & 0 deletions src/algorithms/sorting/cycle-sort/__test__/CycleSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import CycleSort from '../CycleSort';
import {
equalArr,
notSortedArr,
reverseArr,
sortedArr,
SortTester,
} from '../../SortTester';

describe('CycleSort', () => {
it('should sort array', () => {
SortTester.testSort(CycleSort);
});

it('should sort array with custom comparator', () => {
SortTester.testSortWithCustomComparator(CycleSort);
});

it('should do stable sorting', () => {
SortTester.testSortStability(CycleSort);
});
});