Cycle Sort
Cycle sort is a comparison sorting algorithm which forces array to be factored into the number of cycles where each of them can be rotated to produce a sorted array. It is theoretically optimal in the sense that it reduces the number of writes to the original array.
Algorithm
Consider an array of n distinct elements. An element a is given, index of a can be calculated by counting the number of elements that are smaller than a.
- if the element is found to be at its correct position, simply leave it as it is.
- Otherwise, find the correct position of a by counting the total number of elements that are less than a. where it must be present in the sorted array. The other element b which is replaced is to be moved to its correct position. This process continues until we got an element at the original position of a.
The illustrated process constitutes a cycle. Repeating this cycle for each element of the list. The resulting list will be sorted.
C program
- #include<stdio.h>
- void sort(int a[], int n)
- {
- int writes = 0,start,element,pos,temp,i;
- for (start = 0; start <= n - 2; start++) {
- element = a[start];
- pos = start;
- for (i = start + 1; i < n; i++)
- if (a[i] < element)
- pos++;
- if (pos == start)
- continue;
- while (element == a[pos])
- pos += 1;
- if (pos != start) {
- //swap(element, a[pos]);
- temp = element;
- element = a[pos];
- a[pos] = temp;
- writes++;
- }
- while (pos != start) {
- pos = start;
- for (i = start + 1; i < n; i++)
- if (a[i] < element)
- pos += 1;
- while (element == a[pos])
- pos += 1;
- if (element != a[pos]) {
- temp = element;
- element = a[pos];
- a[pos] = temp;
- writes++;
- }
- }
- }
- }
- int main()
- {
- int a[] = {1, 9, 2, 4, 2, 10, 45, 3, 2};
- int n = sizeof(a) / sizeof(a[0]),i;
- sort(a, n);
- printf("After sort, array : ");
- for (i = 0; i < n; i++)
- printf("%d ",a[i]);
- return 0;
- }
Output:
After sort, array : 1 2 2 2 3 4 9 10 45
Java Program
- public class CycleSort
- {
- static void sort(int a[], int n)
- {
- int writes = 0,start,element,pos,temp,i;
- for (start = 0; start <= n - 2; start++) {
- element = a[start];
- pos = start;
- for (i = start + 1; i < n; i++)
- if (a[i] < element)
- pos++;
- if (pos == start)
- continue;
- while (element == a[pos])
- pos += 1;
- if (pos != start) {
- //swap(element, a[pos]);
- temp = element;
- element = a[pos];
- a[pos] = temp;
- writes++;
- }
- while (pos != start) {
- pos = start;
- for (i = start + 1; i < n; i++)
- if (a[i] < element)
- pos += 1;
- while (element == a[pos])
- pos += 1;
- if (element != a[pos]) {
- temp = element;
- element = a[pos];
- a[pos] = temp;
- writes++;
- }
- }
- }
- }
- public static void main(String[] args)
- {
- int a[] = { 1, 9, 2, 4, 2, 10, 45, 3, 2 };
- int n = a.length,i;
- sort(a, n);
- System.out.println("After sort, array : ");
- for (i = 0; i < n; i++)
- System.out.println(a[i]);
- }
- }
Output:
After sort, array : 1 2 2 2 3 4 9 10 45
C# Program
- using System;
- public class CycleSort
- {
- static void sort(int[] a, int n)
- {
- int writes = 0,start,element,pos,temp,i;
- for (start = 0; start <= n - 2; start++) {
- element = a[start];
- pos = start;
- for (i = start + 1; i < n; i++)
- if (a[i] < element)
- pos++;
- if (pos == start)
- continue;
- while (element == a[pos])
- pos += 1;
- if (pos != start) {
- //swap(element, a[pos]);
- temp = element;
- element = a[pos];
- a[pos] = temp;
- writes++;
- }
- while (pos != start) {
- pos = start;
- for (i = start + 1; i < n; i++)
- if (a[i] < element)
- pos += 1;
- while (element == a[pos])
- pos += 1;
- if (element != a[pos]) {
- temp = element;
- element = a[pos];
- a[pos] = temp;
- writes++;
- }
- }
- }
- }
- public void Main()
- {
- int[] a = { 1, 9, 2, 4, 2, 10, 45, 3, 2 };
- int n = a.Length,i;
- sort(a, n);
- Console.WriteLine("After sort, array : ");
- for (i = 0; i < n; i++)
- Console.WriteLine(a[i]);
- }
- }
Output:
After sort, array : 1 2 2 2 3 4 9 10 45