Hubbry Logo
search
logo
898044

Cycle sort

logo
Community Hub0 Subscribers
Write something...
Be the first to start a discussion here.
Be the first to start a discussion here.
See all
Cycle sort

Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array, unlike any other in-place sorting algorithm. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sorted result.

Unlike nearly every other sort, items are never written elsewhere in the array simply to push them out of the way of the action. Each value is either written zero times, if it is already in its correct position, or written one time to its correct position. This matches the minimal number of overwrites required for a completed in-place sort.

Minimizing the number of writes is useful when making writes to some huge data set is very expensive, such as with EEPROMs like Flash memory where each write reduces the lifespan of the memory.[citation needed]

To illustrate the idea of cycle sort, consider a list with distinct elements. Given an element , we can find the index at which it will occur in the sorted list by simply counting the number of elements in the entire list that are smaller than . Now

Repeating this process for every element sorts the list, with a single writing operation if and only if an element is not already at its correct position. While computing the correct positions takes time for every single element, thus resulting in a quadratic time algorithm, the number of writing operations is minimized.

To create a working implementation from the above outline, two issues need to be addressed:

The following Python implementation[circular reference] performs cycle sort on an array, counting the number of writes to that array that were needed to sort it.

Python

See all
User Avatar
No comments yet.