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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generic Linked List in C
- When does the worst case of Quicksort occur?
- QuickSort on Doubly Linked List
- QuickSort on Singly Linked List
- Can QuickSort be implemented in O(nLogn) worst case time complexity?
- 3-Way QuickSort (Dutch National Flag)
- QuickSort Tail Call Optimization (Reducing worst case space to Log n )
- Hoare's vs Lomuto partition scheme in QuickSort
- Why quicksort is better than mergesort ?
- Dual pivot Quicksort
- C++ Program for QuickSort
- Java Program for QuickSort
- Python Program for QuickSort
- Comparisons involved in Modified Quicksort Using Merge Sort Tree
- Stable QuickSort
- QuickSort using Random Pivoting
- Merge two sorted arrays in O(1) extra space using QuickSort partition
- UDP Server-Client implementation in C
- UDP Client Server using connect | C implementation
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.