Write a function to implement quicksort algorithm that will work for all types of data i.e ints, floats, chars etc.
It should accept all types of data and show the sorted data as output.
Note: This function is similar to C standard library function qsort().
First Input as a string. Input :abc cad bcd xyz bsd Output :abc bcd bsd cad xyz Second input as integer Input :5 6 4 2 3 Output :2 3 4 5 6
We use void* to implement generic quicksort function in C. void* does not know how much bytes of memory it has to occupy in memory space. It must be casted to any other data type like int*, char* before doing any operation on it.
Example: when we declare int var; compiler knows that it has occupy 4 bytes of memory but void does not know how much bytes of memory it has to occupy.
We will also use a pointer to function that will point to a function which is dependent to different types of data i.e and this function will be defined by the user according to there need.
Below is the image representation of void* in memory before and after casting it to any particular data type for better understanding.
Void* pt in Memory :
void* pt casted to char* :
afg bbc def ede hello hmmm how okay xcd 23 44 45 65 70 78 89
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Generic Linked List in C
- C++ Program for QuickSort
- Why quicksort is better than mergesort ?
- Stable QuickSort
- QuickSort using Random Pivoting
- Python Program for QuickSort
- Java Program for QuickSort
- Dual pivot Quicksort
- When does the worst case of Quicksort occur?
- QuickSort on Doubly Linked List
- QuickSort on Singly Linked List
- 3-Way QuickSort (Dutch National Flag)
- Hoare's vs Lomuto partition scheme in QuickSort
- Comparisons involved in Modified Quicksort Using Merge Sort Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.