Shaker Sort

Shaker Sort
This commit is contained in:
Chetan Kaushik 2017-05-05 00:17:07 +05:30 committed by GitHub
commit 80496da206
1 changed files with 35 additions and 0 deletions

35
sorting/shaker_sort.c Normal file
View File

@ -0,0 +1,35 @@
#include <stdio.h>
void swap(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void shakersort(int a[], int n)
{
int p, i;
for (p = 1; p <= n / 2; p++)
{
for (i = p - 1; i < n - p; i++)
if (a[i] > a[i+1]){
swap(&a[i], &a[i + 1]);
}
for (i = n - p - 1; i >= p; i--)
if (a[i] < a[i-1]){
swap(&a[i], &a[i - 1]);
}
}
}
int main()
{
int n;
scanf("%d",&n);
int arr[n] ,i;
for (i = 0 ; i < n; i++)
scanf("%d ", &arr[i]);
shakersort(arr, n);
for (i = 0 ; i < n; i++)
printf("%d ", arr[i]);
return 0;
}