Quote:
it compiles but the Windows stops working
That is not a good problem description. Windows is an operating system and it will usually not "stop working" when running a program that behaves unpredictable.
Do you mean your application (main window) is not responding anymore?
Then use a debugger to run your application step by step and inspect your variables.
You should have shown also the code of the
merge()
function and how you call the sort function.
However, there are at least two big mistakes and two others in your code
The first big one:
int size= sizeof(arr)/sizeof(arr[0]);
arr
is of type
int*
so that
sizeof(arr)
is 4 or 8 depending on the application type (32 or 64 bit). Dividing that by
sizeof(int)
results in
size
being
always 1 or 2.
The solution is to add a function parameter specifying the number of items in the array:
int* mergeSort(int* arr, int items);
The second big one and others:
int middle= size/2;
int* rightArr= (int*)malloc(sizeof(int)*middle);
for(int j=middle+1; j<size; j++){
rightArr[j]= arr[j];
}
rightArr
has
middle
(
size/2
) items but you are assigning values starting at
middle+1
. That is an out of bound array write access!
I would expect something like:
for(int i = 0, j=middle; i<middle; i++, j++){
rightArr[i]= arr[j];
}
Note also that I have changed the start index for
arr
from
middle+1
to
middle
(other mistake).
The final mistake might be (you have to check if it applies) that you are not handling the case of odd sizes.