This is a simple bubble sort using a function pointer for ascending or descending. I don’t understand how the return statement on the ascending/descending function affects the swap.
Maybe I’m reading the return statement wrong on ascending? Does it mean return b if it’s less than a? Or does it mean return 0 or 1 if the statement is true? Could use some clarification. Thanks.
void bubble( int work[], const int size, int (*compare)(int a, int b)){
int pass; /* pass counter */
int count;
void swap(int *element1Ptr, int *element2Ptr);
for ( pass = 1 pass < size; pass++){
/* loop to control number of comparison per pass */
if ((*compare)(work[count], work[count+1])){
swap(&work[count], &work[count + 1]);
}
}
}
void swap ( int *element1Ptr, int *element2Ptr){
int hold;
hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}
/* determine whether elements are out of order for an ascending order sort */
int ascending( int a, int b){
return b < a;
}
int descending( int a, int b){
return b > a
}
A
returnstatement inCreturns the expression given, casted to the return type of the function (if possible). In these cases,b < aanda < bare boolean expressions, which return1or0.In other terms, it essentially means the following, but more concise (for
b < a):