Cycle Sort
   HOME

TheInfoList



OR:

Cycle sort is an in-place,
unstable In dynamical systems instability means that some of the outputs or internal state (controls), states increase with time, without bounds. Not all systems that are not Stability theory, stable are unstable; systems can also be marginal stability ...
sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending ...
, a
comparison sort A comparison sort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation (often a "less than or equal to" operator or a three-way comparison) that determines which of two elements should oc ...
that is theoretically optimal in terms of the total number of writes to the original
array An array is a systematic arrangement of similar objects, usually in rows and columns. Things called an array include: {{TOC right Music * In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
, unlike any other in-place sorting algorithm. It is based on the idea that the
permutation In mathematics, a permutation of a set can mean one of two different things: * an arrangement of its members in a sequence or linear order, or * the act or process of changing the linear order of an ordered set. An example of the first mean ...
to be sorted can be factored into
cycle Cycle, cycles, or cyclic may refer to: Anthropology and social sciences * Cyclic history, a theory of history * Cyclical theory, a theory of American political history associated with Arthur Schlesinger, Sr. * Social cycle, various cycles in ...
s, 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's 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
EEPROM EEPROM or E2PROM (electrically erasable programmable read-only memory) is a type of non-volatile memory. It is used in computers, usually integrated in microcontrollers such as smart cards and remote keyless systems, or as a separate chip d ...
s like
Flash memory Flash memory is an Integrated circuit, electronic Non-volatile memory, non-volatile computer memory storage medium that can be electrically erased and reprogrammed. The two main types of flash memory, NOR flash and NAND flash, are named for t ...
where each write reduces the lifespan of the memory.


Algorithm

To illustrate the idea of cycle sort, consider a list with distinct elements. Given an element x, 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 x. Now # If the element is already at the correct position, do nothing. # If it is not, we will write it to its intended position. That position is inhabited by a different element y, which we then have to move to ''its'' correct position. This process of displacing elements to their correct positions continues until an element is moved to the original position of x. This completes a cycle. 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 O(n) time for every single element, thus resulting in a quadratic time algorithm, the number of writing operations is minimized.


Implementation

To create a working implementation from the above outline, two issues need to be addressed: # When computing the correct positions, we have to make sure not to double-count the first element of the cycle. # If there are duplicate elements present, when we try to move an element x to its correct position, that position might already be inhabited by an x. Simply swapping these would cause the algorithm to cycle indefinitely. Instead, we have to insert the element ''after any of its duplicates''. The following
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (prog ...
implementation :sr:Ciklično sortiranje#Algoritam performs cycle sort on an array, counting the number of writes to that array that were needed to sort it.
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (prog ...
def cycle_sort(array) -> int: """Sort an array in place and return the number of writes.""" writes = 0 # Loop through the array to find cycles to rotate. # Note that the last item will already be sorted after the first n-1 cycles. for cycle_start in range(0, len(array) - 1): item = array ycle_start # Find where to put the item. pos = cycle_start for i in range(cycle_start + 1, len(array)): if array < item: pos += 1 # If the item is already there, this is not a cycle. if pos

cycle_start: continue # Otherwise, put the item there or right after any duplicates. while item

array os pos += 1 array os item = item, array os writes += 1 # Rotate the rest of the cycle. while pos != cycle_start: # Find where to put the item. pos = cycle_start for i in range(cycle_start + 1, len(array)): if array < item: pos += 1 # Put the item there or right after any duplicates. while item

array os pos += 1 array os item = item, array os writes += 1 return writes
The next implementation written in
C++ C++ (, pronounced "C plus plus" and sometimes abbreviated as CPP or CXX) is a high-level, general-purpose programming language created by Danish computer scientist Bjarne Stroustrup. First released in 1985 as an extension of the C programmin ...
simply performs cyclic array sorting. template void cycle_sort(type_array *Array, int array_size)


Situation-specific optimizations

When the array contains only duplicates of a relatively small number of items, a constant-time
perfect hash function In computer science, a perfect hash function for a set is a hash function that maps distinct elements in to a set of integers, with no hash collision, collisions. In mathematical terms, it is an injective function. Perfect hash functions may ...
can greatly speed up finding where to put an item, turning the sort from Θ(''n''2) time to Θ(''n'' + ''k'') time, where ''k'' is the total number of hashes. The array ends up sorted in the order of the hashes, so choosing a hash function that gives you the right ordering is important. Before the sort, create a
histogram A histogram is a visual representation of the frequency distribution, distribution of quantitative data. To construct a histogram, the first step is to Data binning, "bin" (or "bucket") the range of values— divide the entire range of values in ...
, sorted by hash, counting the number of occurrences of each hash in the array. Then create a table with the cumulative sum of each entry in the histogram. The cumulative sum table will then contain the position in the array of each element. The proper place of elements can then be found by a constant-time hashing and cumulative sum table lookup rather than a
linear search In computer science, linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched. A linear search runs in linea ...
.


References


External links


"Cycle-Sort: A Linear Sorting Method", The Computer Journal (1990) 33 (4): 365-367.

Original source of unrestricted variant


{{DEFAULTSORT:Cycle Sort Comparison sorts Articles with example pseudocode Articles with example Python (programming language) code Articles with example C++ code