C Program to Implement CockTail Sort

This C Program implements cocktail sort. Cocktail sort is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from a bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than a bubble sort, and solves the problem of turtles in bubble sorts.
The various names of cocktail sort are bidirectional bubble sort, cocktail shaker sort, shaker sort , ripple sort, shuffle sort, shuttle sort or happy hour sort

Here is source code of the C Program to implement cocktail sort. 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 CockTail Sort
  3.  */
  4. #include <stdio.h>
  5. #define MAX 8
  6.  
  7. int main()
  8. {
  9.     int data[MAX];
  10.     int i, j, n, c;
  11.  
  12.     printf("\nEnter the data");
  13.     for (i = 0; i < MAX; i++)
  14.     {
  15.         scanf("%d", &data[i]);
  16.     }
  17.     n = MAX;    
  18.     do
  19.     {
  20.         /*
  21.           * Rightward pass will shift the largest element to its correct place at the end
  22.          */
  23.         for (i = 0;  i < n - 1; i++)
  24.         {
  25.             if (data[i] > data[i + 1])
  26.             {
  27.                 data[i] = data[i] + data[i + 1];
  28.                 data[i + 1] = data[i] - data[i + 1];
  29.                 data[i] = data[i] - data[i + 1];
  30.  
  31.             }
  32.  
  33.         }
  34.         n = n - 1;
  35.         /* 
  36.           * Leftward pass will shift the smallest element to its correct place at the beginning
  37.           */
  38.         for (i= MAX - 1, c = 0; i >= c; i--)
  39.         {
  40.             if(data[i] < data[i - 1])
  41.             {
  42.                 data[i] = data[i] + data[i - 1];
  43.                 data[i - 1] = data[i] - data[i - 1];
  44.                 data[i] = data[i] - data[i - 1];
  45.             }
  46.         }
  47.         c = c + 1;
  48.  
  49.     } while (n != 0 && c != 0);
  50.     printf("The sorted elements are:");
  51.     for (i = 0; i < MAX; i++)
  52.     {
  53.         printf("%d\t", data[i]);
  54.     }
  55. }

$ gcc cocktailsort.c
$ a.out
/*
 * Average case
 */
Enter the data
9 6 2 12 11 9 3 7
The sorted elements are:2       3       6       7       9       9       11      12  
/*
 * Worst case
 */
 Enter the data
 8 7 6 5 4 3 2 1
 The sorted elements are:1         2        3        4        5        6        7        8
 /*
  *Best case
  */
  Enter the data
  1 2 3 4 5 6 7 8
  The sorted elements are:1     2        3        4        5        6        7        8

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.