/* Straight Merge Sort - Recursive */
#include
#include
#define MAXNOS 100 /* Macro definition for maximum numbers to be input */
void mergesort(int [], int, int); /* sort function prototype declaration */
void merge(int [], int, int, int);
void main(void)
{
int a[MAXNOS]; /* array to hold the input numbers */
int m; /* stores the total count of input numbers */
int i; /* for loop variable */
input : /* label name for transfer of control through goto */
clrscr();
printf("Straight Merge Sort - Recursive\n");
printf("-------- ----- ---- - ---------\n\n");
printf("Enter how many numbers will be input for sorting - ");
flushall();
scanf("%d", &m);
if (m < 2 || m > MAXNOS) /* check for valid input */
{
printf("Error - input value is out of range...");
getch();
goto input;
}
printf("Enter the numbers one by one -\n");
for (i=0; i {
printf("%2d - ", (i+1));
flushall();
scanf("%d", &a[i]);
}
mergesort(a, 0, (m-1)); /* function is invoked for sorting the numbers */
printf("\n\nSorted numbers are - \n");
for (i=0; i printf("%d\t", a[i]); /* sorted numbers are displayed on the screen */
getch();
}
void mergesort(int *x, int lb, int ub)
{ /* Function Definition begins here */
int mid; /* stores the middle of the set to be sorted */
if (lb < ub) /* continue recursion if lb < ub */
{
mid = (lb+ub)/2; /* Calculate the middle of the set */
mergesort(x, lb, mid); /* recursive call for the left half */
mergesort(x, (mid+1), ub); /* recursive call for the right half */
merge(x, lb, mid, ub); /* merge two sets into one new set */
} /* end of if */
} /* End of function mergesort */
void merge(int *x, int lb1, int ub1, int ub2)
{
int aux[MAXNOS] = {0}; /* temporary array used in merging from and to x */
int i, j, k; /* for loop variables */
i = lb1; /* set to lower bound of the first set */
j = (ub1+1); /* set to lower bound of the second set */
k = 0; /* output control variable */
while (i<= ub1 && j <= ub2) /* check if any of the sets is completed */
{
if (x[i] < x[j]) /* compare the two elements of the two sets */
{
aux[k++] = x[i++]; /* transfer the first set element and increment */
}
else
{
aux[k++] = x[j++]; /* transfer the second set element and increment */
}
}
while (i<=ub1)
{
aux[k++] = x[i++]; /* transfer (if any) remaining elements of first set */
}
while (j<=ub2)
{
aux[k++] = x[j++]; /* transfer (if any) remaining elements of second set */
}
for (i=lb1, j=0; i<=ub2; i++,j++)
{
x[i] = aux[j]; /* transfer the merged output array back to x */
}
} /* End of function merge */
#include
#include
#define MAXNOS 100 /* Macro definition for maximum numbers to be input */
void mergesort(int [], int, int); /* sort function prototype declaration */
void merge(int [], int, int, int);
void main(void)
{
int a[MAXNOS]; /* array to hold the input numbers */
int m; /* stores the total count of input numbers */
int i; /* for loop variable */
input : /* label name for transfer of control through goto */
clrscr();
printf("Straight Merge Sort - Recursive\n");
printf("-------- ----- ---- - ---------\n\n");
printf("Enter how many numbers will be input for sorting - ");
flushall();
scanf("%d", &m);
if (m < 2 || m > MAXNOS) /* check for valid input */
{
printf("Error - input value is out of range...");
getch();
goto input;
}
printf("Enter the numbers one by one -\n");
for (i=0; i
printf("%2d - ", (i+1));
flushall();
scanf("%d", &a[i]);
}
mergesort(a, 0, (m-1)); /* function is invoked for sorting the numbers */
printf("\n\nSorted numbers are - \n");
for (i=0; i
getch();
}
void mergesort(int *x, int lb, int ub)
{ /* Function Definition begins here */
int mid; /* stores the middle of the set to be sorted */
if (lb < ub) /* continue recursion if lb < ub */
{
mid = (lb+ub)/2; /* Calculate the middle of the set */
mergesort(x, lb, mid); /* recursive call for the left half */
mergesort(x, (mid+1), ub); /* recursive call for the right half */
merge(x, lb, mid, ub); /* merge two sets into one new set */
} /* end of if */
} /* End of function mergesort */
void merge(int *x, int lb1, int ub1, int ub2)
{
int aux[MAXNOS] = {0}; /* temporary array used in merging from and to x */
int i, j, k; /* for loop variables */
i = lb1; /* set to lower bound of the first set */
j = (ub1+1); /* set to lower bound of the second set */
k = 0; /* output control variable */
while (i<= ub1 && j <= ub2) /* check if any of the sets is completed */
{
if (x[i] < x[j]) /* compare the two elements of the two sets */
{
aux[k++] = x[i++]; /* transfer the first set element and increment */
}
else
{
aux[k++] = x[j++]; /* transfer the second set element and increment */
}
}
while (i<=ub1)
{
aux[k++] = x[i++]; /* transfer (if any) remaining elements of first set */
}
while (j<=ub2)
{
aux[k++] = x[j++]; /* transfer (if any) remaining elements of second set */
}
for (i=lb1, j=0; i<=ub2; i++,j++)
{
x[i] = aux[j]; /* transfer the merged output array back to x */
}
} /* End of function merge */