Built in functions for sorting arrays in C

Viewed 30126

Are there any built in functions in C programming language for sorting arrays? Or do I have to write my own functions?

6 Answers
 #include <stdio.h> 
 #include <stdlib.h> 

  // This function is used in qsort to decide the relative order 
  // of elements at addresses p and q. 
  int comparator(const void *p, const void *q) 
   { 
      return (*(int*)p-*(int*)q);
      } 

  // A utility function to print an array 
  void printArr(int arr[], int n) 
  { 
     int i; 
     for (i = 0; i < n; ++i) 
     printf("%d ", arr[i]); 
  }  

  // Driver program to test above function 
  int main() 
  { 
     int arr[] = {1, 6, 5, 2, 3, 9, 4, 7, 8, 0}; 
     int size = sizeof(arr) / sizeof(arr[0]); 
     qsort((void*)arr, size, sizeof(arr[0]), comparator); 
     printf("Output array is\n"); 
     printArr(arr, size);  
     return 0; 
   } 
Related