I made a merge sort function:
void mergeSort(int emotionCount[], int low, int high){
int i=0,k=0;
//I did this to see the value inside the array, and I always got a garbage value
//when i=0, and the first correct value when i=1. I made a for loop here to
//see the values in the array in debugging mode in netbean.
for (i=0;i<=high;i++){
}
if (low == high){
emotionCount[low]=emotionCount[low];
}else{
int mid = (low+high)/2;
mergeSort(emotionCount,low,mid);
mergeSort(emotionCount,mid+1,high);
merge(emotionCount, low,mid, high); }
}
void merge(int emotionCount[], int low,int mid, int high)
{
int temp[high-low+1];
int i=low,k=mid+1, j=high, n=0;
int comparing=emotionCount[k];
while (i<=mid || k<=high){
while (emotionCount[i]<comparing)
{
temp[n]=emotionCount[i];
i++;
n++;
}
comparing=emotionCount[i];
temp[n]=emotionCount[k];
k++;
i++;
n++;
while (emotionCount[k]<comparing){
temp[n]=emotionCount[k];
k++;
n++;
}
comparing=emotionCount[k];
temp[n]=emotionCount[i];
k++;
i++;
n++;
}
while (i<=mid)
{
temp[n]=emotionCount[i];
i++;
n++;
}
while (k<=high)
{
temp[n]=emotionCount[k];
k++;
n++;
}
while (low<=high)
{
emotionCount[low]=temp[i];
low++;
}
}
And in main, i pass an array:
int array[10] = {0,5,8,2,4,6,8,2,20,25};
//number 9 because the highest position is 9. After this, array[10] is supposed to be sorted.
mergeSort(array ,0, 9);
Well, the method was kinda long, but I want to implement it myself. Basically, I pass an array to the mergeSort function, and if it is not at its minimum size (which is 1), it will continue to pass that array. The thing which bugging me is, when I pass an array to it, the first value is always a garbage value (like an address value or something). Only after i=1, it will give the first value of the array. I don't get it. Also, everytime exiting a lower mergeSort function to continue on the higer ones, all the sorted values in the array become 0.
Edit: The k variable is just used to hold the array value, to see the array value when I'm in debugging mode. I removed the k variable in the mergeSort function to clear up the confusion.