Sorting Program
Sorting Program
// Recursive calls
mergeSort(left, mid);
mergeSort(right, length - mid);
/*
* This loop will loop through both arrays and look
* at the lowest index value in the array, which will
* be the lowest value. It copares the left value and
* the right value and takes the lowest one and adds
* it to current. Then it incriments up the index for
* the array where it just took the value.
*
*/
int i = 0, j = 0, k = 0;
while (i < leftSize && j < rightSize) {
if (left[i] <= right[j]) {
current[k] = left[i];
k++;
i++;
}
else {
current[k] = right[j];
k++;
j++;
}
}
/*
* Since we didn't take the values out evenly, one of the
* arrays may still have values remaining. These calls
* will place the remaining values in the current array.
*/
while (i < leftSize) {
current[k] = left[i];
k++;
i++;
}
while (j < rightSize) {
current[k] = right[j];
k++;
j++;
}
/*
* Notice that we do not have a return value anywhere. This is
* because we are using the pointer values to the arrays. As a
* result, updates in the method update the original arrays as well.
*/
}
}