C Programming Code Examples C > Sorting Searching Code Examples C Program to Implement Cyclesort C Program to Implement Cyclesort This C Program implements cyclesort. 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. #include <stdio.h> #define MAX 8 void cycle_sort(int *); void main() { int a[MAX],x; printf("enter the elements into array :"); for (x = 0;x < MAX; x++) { scanf("%d", &a[x]); } cycle_sort(a); printf("sorted elements are :\n"); for (x = 0;x < MAX; x++) { printf("%d", a[x]); } } /* sorts elements using cycle sort algorithm */ void cycle_sort(int * a) { int temp, item, pos, x, j, k; for (x = 0;x < MAX; x++) { item = a[x]; pos = x; do { k = 0; for (j = 0;j < MAX;j++) { if (pos != j && a[j] < item) { k++; } } if (pos != k) { while (pos != k && item == a[k]) { k++; } temp = a[k]; a[k] = item; item = temp; pos = k; } }while (pos != x); } }