Code:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
void sort(int array[],int size, int comp);
static bool mergeruns(int a[], int b[], int n, int comp);
void merge(int a[], int b[], int lo, int hi, bool asc, int comp);
void naturalmergesort(int a[], int b[], int n, int comp);
int main(int argc, char **argv)
{
int array[50];
int item,j, i=0;
static int comp=0;
while(scanf("%d", &item) > 0)
{
array[i] = item;
i++;
}
sort(array,i,comp);
for(j=0; j < i; j++)
printf("%d ", array[j]);
printf("\n");
printf("%d comparisons made\n", comp);
return 0;
}
void sort(int array[],int size, int comp)
{
int b[50];
int n=size;
naturalmergesort(array, b, n, comp);
}
static bool mergeruns(int a[], int b[], int n, int comp)
{
int i=0, k=0, x;
bool asc=true;
while (i<n)
{
k=i;
do x=a[i++]; while (i<n && x<=a[i]);
while (i<n && x>=a[i]) x=a[i++];
merge (a, b, k, i-1, asc, comp);
asc=!asc;
}
return k==0;
}
void merge(int a[], int b[], int lo, int hi, bool asc, int comp)
{
int l = lo; int r = hi;
int k=asc ? l : r;
int c=asc ? 1 : -1;
int i=lo, j=hi;
while (i<=j)
{
if (a[i]<=a[j])
{
b[k]=a[i++];
comp++;
}
else
{
b[k]=a[j--];
comp++;
}
k+=c;
}
}
void naturalmergesort(int a[], int b[], int n, int comp)
{
while (!mergeruns(a, b, n, comp) & !mergeruns(b, a, n, comp));
}