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.

Here is source code of the C Program to implement cyclesort. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to Implement Cyclesort 
  3.  */
  4. #include <stdio.h>
  5.  
  6. #define MAX 8
  7.  
  8. void cycle_sort(int *);
  9.  
  10. void main()
  11. {
  12.     int a[MAX],i;
  13.  
  14.     printf("enter the elements into array :");
  15.     for (i = 0;i < MAX; i++)
  16.     {
  17.         scanf("%d", &a[i]);
  18.     }
  19.     cycle_sort(a);
  20.     printf("sorted elements are :\n");
  21.     for (i = 0;i < MAX; i++)
  22.     {
  23.         printf("%d", a[i]);
  24.     }
  25. }
  26.  
  27. /* sorts elements using cycle sort algorithm */
  28. void cycle_sort(int * a)
  29. {
  30.     int temp, item, pos, i, j, k;
  31.  
  32.     for (i = 0;i < MAX; i++)
  33.     {
  34.         item = a[i];
  35.         pos = i;
  36.         do
  37.         {
  38.             k = 0;
  39.             for (j = 0;j < MAX;j++)
  40.             {
  41.                 if (pos != j && a[j] < item)
  42.                 {
  43.                     k++;
  44.                 }
  45.             }
  46.             if (pos != k)
  47.             {
  48.                 while (pos != k && item == a[k])
  49.                 {
  50.                     k++;
  51.                 }
  52.                 temp = a[k];
  53.                 a[k] = item;
  54.                 item = temp;
  55.                 pos = k;
  56.             }
  57.         }while (pos != i);
  58.     }
  59. }

$ cc cyclesort.c
$ a.out
enter the elements into array :7 3 2 5 4 8 9 6
sorted elements are :
23456789
$ a.out
enter the elements into array :7 3 2 4 5 4 6 3
sorted elements are :
23344567

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.