Friday, 28 February 2014

Cycle Sort

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'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.
Example of cycle sort sorting a list of random numbers.
Class
Data structure
Θ(n2)
Θ(n2)
Θ(n2)
Θ(n) total, Θ(1) auxiliary
Algorithm

1. H[l] <— 0;
2. for k <— 1 to m-1 do H[k+1] <— H[k]+h[k];
3. for i <— 1 to n do
4. k <— A[i] ; j <— H[k]+1;
5. if i>=j then
6. if i<>j then
7. temp <— A[i] ;
8. repeat
9. swap(A[j],temp);
10. H[k] <— j; k <— temp; j <— H[k]+1;
11. until i=j;
12. A[i] <— temp;
13. H[k] <— I;

No comments:

Post a Comment