Quicksort algorithm's incomplete implementation in book, in the C language.
Want to generate a complete code to the program for Quicksort code, given incomplete in the book: Algorithms in a nutshell, by George T. Heineman.
The book states the below parts, leaving out the main(), cmp(), selectPivotIndex functions' implementation to the user. Code:
void sortPointers(void **vals, int total_elems, int(*cmp)(const void *, const void *)) The use of: void **vals, as parameter for passing array, seems confusing to me; and request what is the reason for using a double indirection pointer. I mean the job can be done by a single indirection pointer too, then why was a double indirection pointer needed? |
When you need to access the individual pointers within the pointer-array, you can write simply:
Code:
void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right) { Code:
void do_qsort(void *ar, int(*cmp)(const void *, const void *), int left, int right) { Code:
void do_qsort(void *ar[], int(*cmp)(const void *, const void *), int left, int right) { Code:
typedef void *AnyPtr; |
Quote:
void **vals means: vals is a pointer, and the location (where it points) contains also a pointer. So you declare: vals is a pointer to a pointer From the other hand: void *vals means vals is just a pointer, but we don't know anything about that location (if it points to a string, integer or what?). |
Quote:
Seems, the book is using an array of pointers instead; and those pointers in different locations of the array, point to integers. |
Quote:
It seems from your answer that an array of integers, is not considered in the functions given; or an array of integers is itself an array of pointers! Am confused as never thought either of the two options to be true, i.e. neither am clear that the given functions operate on an array of pointers; or the second option, which is a fundamental question - i.e. if the array of integers, is implemented as an array of pointers? |
Imagine that instead of ints, you're sorting a bunch of large data structures. That's a lot of data to be swapping back and forth, so it makes sense to take the approach of sorting an index array of pointers to those objects rather than throwing the objects themselves about. That said you will need the unsorted index to exist before you start, but in most real-world cases, your code most likely already has one for other reasons.
|
Quote:
Next, is unclear why the sortPointers function is being used? Its use seems redundant, and unnecessary to me, and just the function do_qsort should be directly called by the main(). |
Hi
I agree the void ** parameter is a bit confusing. And it doesn't allow other things than pointers to be sorted. I searched, and this is a little better: https://www.geeksforgeeks.org/generi...lgorithm-in-c/ Here they cast void* to char* so we can do "math" with the pointers. This allows sorting arrays of anything - not just pointers. Notice they also have a function for swap - you need it for big structures. But you still need a comparison function. It has 2 pointers as parameters. I don't think its possible to make generic functions taking parameters without a type. And it wouldn't always for big structures. |
Quote:
It needs a list of pointers and a comparator function and will only call do_qsort. Obviously you can call it directly, without sortPointers. |
yeah that's show you how to do things books for you, they're incomplete, you'd probably have better luck googling it
https://hackr.io/blog/quick-sort-in-c https://www.geeksforgeeks.org/quick-sort/ |
Sorting arrays of pointers covers 95% of cases (actual count, not a made-up statistic).
A double indirection allows the same parameters to be passed to the swap and comparison functions if one has both. Ed |
Quote:
And we just reorder the pointers, not the structs themselves. |
Quote:
If possible give an smallish example, for emphasizing. |
Passing the compare function as a parameter makes the C library quicksort generic. A fully generic quicksort would also be passed a swap function.
Ed |
this is all about to have a generic solution. The implemented qsort knows nothing about the structure of the data behind the pointers, therefore you need to pass a list (of pointers) to it and a comparator function which knows exactly how can be the real data evaluated (compared) using those pointers.
The sorting algorithm itself is implemented in the qsort function and the real data type, data structure is completely irrelevant here. Obviously you can do that in one single function, with embedded comparator functionality, without real cmp function, this is also implemented and used in special cases (for example when the speed is very important). |
All times are GMT -5. The time now is 07:52 PM. |