Sort array in the wave form C Program

A array is given and sort it in the wave form. Wave form of a array: If array is {1,4,2,7,8,4,9,3} then sort it like {4,1,7,2,8,4,9,3}. Like arr[0]>=arr[1]<=arr[2]>=arr[3]<=arr[4]....


/* Sort array in the wave form */
#include<stdio.h>
void Swap(int *a, int *b)
{
	int temp=*a;
	*a=*b;
	*b=temp;
}
void Sort(int arr[],int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		if(i>0&&arr[i]>arr[i-1])
		{
			Swap(&arr[i],&arr[i-1]);
		}
		if(i<n-1&&arr[i]>arr[i+1])
		{
			Swap(&arr[i],&arr[i+1]);
		}
	}
}
int main()
{
	int arr[]={2,4,3,8,1,9,3,7};
	int i,n=sizeof(arr)/sizeof(arr[0]);
 
	Sort(arr,n);
 
	for(i=0;i<n;i++)
	{
		printf("%d ",arr[i]);
 
	}
 
	return 0;
}

Popular posts from this blog