![]() |
In C, we may sometimes need to sort the array of structs based on the values of certain members. In this article, we will learn how to sort a given array of structures based on a specific member in C. For Example, Input: Sorting Array of Structure based on a Member in CTo sort an array of structures based on a specific member, we can use the qsort() library function. Pass a pointer to an array, the number of elements, the size of each element, and a comparison function that tells qsort how to compare two structs based on that members. C Program to Sort an Array of Structure based on Specific MemberThe below example demonstrates how we can sort an array of structures on the basis of certain members using qsort in C. C
Output
Sorted array based on age: Person2, 20, 175.0 Person1, 21, 160.5 Person3, 22, 165.5 Time Complexity: O(n log n), time complexity of the quicksort algorithm used by |
Reffered: https://www.geeksforgeeks.org
C Language |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |