Handle size 0
// if (num_elements == 1) {
if (num_elements <= 1) {
return;
}
Sorting double should handle not-a-number
Research isgreater(), isgreaterequal(), ... to well compare FP when NANs are involved.
IMO, all NANs should get sorted to the end of the array.
Minor: Use memcpy() when able
//for (int i = 0; i < num_elements_A; ++i) {
// A[i] = array[i];
//}
memcpy(A, array, sizeof A);
Consider size_t to handle arrays of all sizes
// void sort(int num_elements, double array[num_elements]) {
void sort(size_t num_elements, double array[num_elements]) {
Note: This is a minor issue as long as temporary space is local to the function. For me, once n == 100,000, I ran out of stack space.
Make cleaner faster code with 3that executes 2 loops
As suggested by @AShellyReduce index testing. Something like:
The last 2 loops replace with memcpy().
size_t left = 0;
size_t right = 0;
size_t i = 0;
while (1) {
if (fcmpd(A[left], B[right]) < 0) {
array[i++] = A[left++];
if (left >= nmemb_left) {
memcpy(&array[i], &B[right], sizeof array[0] * (nmemb_right - right));
break;
}
} else {
array[i++] = B[right++];
if (right >= nmemb_right) {
memcpy(&array[i], &A[left], sizeof array[0] * (nmemb_left - left));
break;
}
}
}
Advanced: Sorting double issues: +0, -0, NAN
If code wants all -0.0 < +0.0, more tests needed. Research signbit().
If code wants to sort NANs, consider memcmp().
General
A generic sort would pass in the compare function rather than embed that test within the sort function.