C Program to Implement Shaker Sort

C Program to Implement Shaker Sort

In this tutorial, you will see C Program to Implement Shaker Sort. This is a part of C Programming and Data Structure. Shaker Sort is also known as Cocktail Sort, which is basically a variation of Bubble Sort.

Shaker Sort C Program Code

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10]={110,24,87,12,74,33,47,69,95,52};
int c,sort,w1,w2,temp,i;
c=0;
sort=0;
printf("Given Original Array: \n");
for(i=0;i<10;i++)
printf("%d ",arr[i]);
while(c<3 && sort==0)
{
w1=9;
w2=c;
sort=1;
while(w1>c)
{
if(arr[w1]<arr[w1-1])
{
sort=0;
temp=arr[w1];
arr[w1]=arr[w1-1];
arr[w1-1]=temp;
}
if(arr[w2]>arr[w2+1])
{
sort=0;
temp=arr[w2];
arr[w2]=arr[w2+1];
arr[w2+1]=temp;
}
w2++;
w1--;
}
c++;
printf("\n\nAfter Passing %d of Shaker Sort:\n",c);
for(i=0;i<10;i++)
printf("%d ",arr[i]);
}
getch();
}

Output of Program

Given Original Array: 
110 24 87 12 74 33 47 69 95 52

After Passing 1 of Shaker Sort:
12 24 87 33 74 47 52 69 95 110

After Passing 2 of Shaker Sort:
12 24 33 47 74 52 69 87 95 110

After Passing 3 of Shaker Sort:
12 24 33 47 52 69 74 87 95 110

Program Details

This program is started with including stdio.h and conio.h which are built-in library functions in C Program. The entry point of the program void main is declared. An array is declared containing 10 elements. The variables used are c, sort, w1, w2, temp and i. The initial value of c and sort is zero. The original given array is printed using for loop. While loop is used to check conditions from variable c and sort. Default values of w1 and w2 are declared and a while loop condition is checked. To store the temporary value temp variable is used. New values of the array is also stored. After performing the required operations, new array values are displayed as output.


This tutorial on C Program to Implement Shaker Sort is contributed by Rajnish Kumar. If you like TheCode11 and would like to contribute, you can also write your article and mail to thecode11info@gmail.com

Previous Post Next Post

Contact Form