I need help with mergesort
I used an algoritm form a book I have and have looked through the code but can't seem to understand what is wrong.
Can anyone help? I feel that I am close
any help is much appreciated
Code:
#include <stdio.h>
#include <stdlib.h>
#define count 10000//count is maximum allowable array size
void mergesort(int[], int, int);
void merge(int[], int, int, int);
int main()
{
int x,amount, array[count];
//user prompt
printf("Enter the number of integers you would like to sort\n(no larger than %d): ", count);
scanf("%d",&amount);
//fills array with an amount of random integers decided by user, and prints the array
printf("\n\n Unsorted array: \n");
for (x = 1; x <= amount; x++)
{
array[x] = rand() % count;
printf("%d\t",array[x]);
}
printf("here x= %d",x);//print test
mergesort(array, 0, amount);
printf("here");//print test
//prints the sorted array to screen
printf("\n\n Sorted array: \n");
for (x = 1; x <= amount; x++)
{
printf("%d\t",array[x]);
}
printf("\n\n");
return 0;
}
void mergesort(int array[], int l, int r)
{
if (r <= 1)
{
return;
}
int m = (r+1)/2;
mergesort(array, l, m);
mergesort(array, m+1, r);
merge(array, l, m, r);
}
void merge(int a[], int l, int m, int r)
{
int i, j;
static aux[count];
for(i = m+1; i > 1; i--)
{
aux[i-1] = a[i-1];
}
for(j= m; j < r; j++)
{
aux[r+m-j] = a[j+1];
}
for(int k = 1; k<= r; k++)
{
if(aux[j] < aux[i])
{
a[k] = aux[j--];
}
else
{
a[k] = aux[i++];
}
}
}