C Program to Implement Qsort using Function Pointers

This C Program implements qsort using function pointers.

Here is source code of the C Program implements qsort using function pointers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to Implement qsort using function pointers
  3.  */
  4. #include <stdio.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. struct s
  10. {
  11.     char empname[5];
  12.     int empid;
  13. };
  14.  
  15. /* To sort array elemets */
  16. int int_call(const void *a1,const void *b1)
  17. {
  18.     const int *a = (const int *)a1;
  19.     const int *b = (const int *)b1;
  20.  
  21.     if (*a > *b)
  22.         return 1;
  23.     else
  24.     {
  25.         if (*a == *b) 
  26.             return 0;
  27.         else
  28.             return -1;
  29.     }
  30. }
  31.  
  32. /* To sort structure elemets */
  33. int string_call(const void *a1, const void *b1)
  34. {
  35.     const char *a = (const char *)a1;
  36.     const char *b = (const char *)b1;
  37.     return(strcmp(a, b));
  38. }
  39.  
  40. void main()
  41. {
  42.     int array1[5]={20, 30, 50, 60, 10};
  43.     struct s emprec[5];
  44.     int i, j;
  45.  
  46.     strcpy(emprec[0].empname, "bbb");
  47.     emprec[0].empid = 100;
  48.     strcpy(emprec[1].empname, "ccc");
  49.     emprec[1].empid = 200;
  50.     strcpy(emprec[2].empname, "eee");
  51.     emprec[2].empid = 300;
  52.     strcpy(emprec[3].empname, "aaa");
  53.     emprec[3].empid = 400;
  54.     strcpy(emprec[4].empname,"ddd");
  55.     emprec[4].empid = 500;
  56.     qsort(array1, 5, sizeof(int), int_call);
  57.     qsort(emprec, 5, sizeof(struct s), string_call);
  58.     for (i = 0; i < 5; i++)
  59.         printf("%d\t", array1[i]);
  60.     printf("\nSorting of Structure elements ");
  61.     for (i = 0; i < 5; i++)
  62.         printf("\n%s\t%d", emprec[i].empname, emprec[i].empid);
  63.     printf("\n");
  64. }

$ cc qsort_fp.c
$ a.out
10    20    30    50    60    
Sorting of Structure elements 
aaa    400
bbb    100
ccc    200
ddd    500
eee    300

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.