Ok so far you haven't even shown any code with respect to any sorting.
qsort is a function which performs a quicksort using a user supplied function for comparison. That's right, you are still responsible for handling the comparison between two elements. The qsort function however handles all the iterating through the elements and storing the final sorted value. Let's have a look at the function definition:
Code:
void qsort (
void* base,
size_t num,
size_t width,
int(__cdecl*compare )(const void *, const void*);
Now it is not as complicated as it looks to begin with. Lets start with
the first parameter, void* base. All this is a pointer to the array that you are sorting. Now the array can contain pointers, or anything you want. That is why there is the userdefined compare function. This way you aren't restricted on what qsort can handle. This is also why you must typecast your pointer here to type void.
The second parameter, size_t num, is pretty easy to understand. It is simply the number of elements in your array to be sorted. So if you had an array of 5 names, each name 10 characters in length(aka)the number of elements that you wish to sort is 5.
The third parameter, size_t width, is simply the size of the element in bytes. So using the above example the size of each element would be sizeof(char)*10.
Now the fourth parameter is usually where some confusion sets in although it really is as easy as the other three parameters. This parameter is a pointer to the user defined comparison function. There are just some requirements for this function:
1. The function must return an integer value in the following form:
< 0 element1 is less than element2
= 0 element1 is equal to element2
> 0 element1 is greater than element2
2. The parameters passed to the function are in the form of const void*. So thus a function declaration for a compare function used by qsort would look something like:
Code:
int myCompareFunction(const void*, const void*);
3. Since the parameters are passed to your compare function as type void* you must typecast them to their appropriate type in order to perform the comparison. So if we wanted to compare integers our comparison portion might look like:
Code:
if(*(int*)element1 < *(int*)element2)
return -1;
Now enough with all this talking. Time to wrap this up by looking at a fully functional program that sorts 3 names.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int cmpFunction(const void*, const void*);
int main(void) {
char nameArray[3][10];
strcpy(nameArray[0],"Chris");
strcpy(nameArray[1],"Zambo");
strcpy(nameArray[2], "Abba");
printf("Current array is: \n");
for(int i = 0; i < 3; i++){
printf("%d. %s\n", i, nameArray[i]);
}
qsort((void*)nameArray, // target - pointer to data to be sorted
3, // Array size in elements
sizeof(char)*10, // Element size in bytes
cmpFunction); //Pointer to comparison function
printf("\nSorted array is: \n");
for(int i = 0; i < 3; i++){
printf("%d. %s\n", i, nameArray[i]);
}
return 0;
}
int cmpFunction(const void* name1, const void* name2) {
return strcmp((char*)name1, (char*)name2);
}
I hope this helps you and any else looking at this thread for that matter.
Happy Coding!!