C library function for sorting

Is there any library function available in the standard C library for sorting?

+75
c sorting
Nov 24 '09 at 5:29
source share
7 answers

qsort() is the function you are looking for. You call it with a pointer to your data array, the number of elements in this array, the size of each element and the comparison function.

It does its magic, and your array is sorted in place. The following is an example:

 #include <stdio.h> #include <stdlib.h> int comp (const void * elem1, const void * elem2) { int f = *((int*)elem1); int s = *((int*)elem2); if (f > s) return 1; if (f < s) return -1; return 0; } int main(int argc, char* argv[]) { int x[] = {4,5,2,3,1,0,9,8,6,7}; qsort (x, sizeof(x)/sizeof(*x), sizeof(*x), comp); for (int i = 0 ; i < 10 ; i++) printf ("%d ", x[i]); return 0; } 
+95
Nov 24 '09 at 5:42
source share

The standard C / C ++ library <stdlib.h> contains the qsort function.

This is not the best quicksort in the world, but it is fast and VERY EASY to use ... formal qsort syntax:

 qsort(<arrayname>,<size>,sizeof(<elementsize>),compare_function); 

The only thing you need to implement is compare_function, which takes two arguments of type "const void" that can be assigned to the corresponding data structure, and then return one of these three values:

  • negative if a should be before b
  • 0 if a is equal to b
  • positive if a should be after b

1. Comparison of the list of integers :

just add a and b to integers if x < y , xy negative, x == y , xy = 0 , x > y , xy is positive xy is a quick way to do this :) reverse *x - *y to *y - *x to sort in descending / reverse order

 int compare_function(const void *a,const void *b) { int *x = (int *) a; int *y = (int *) b; return *x - *y; } 

2. Comparison of the list of lines :

To compare a string, you need the strcmp function inside the <string.h> lib. strcmp will by default return -ve, 0, ve appropriately ... sort in reverse order, just undo the character returned by strcmp

 #include <string.h> int compare_function(const void *a,const void *b) { return (strcmp((char *)a,(char *)b)); } 

3. Comparison of floating point numbers :

 int compare_function(const void *a,const void *b) { double *x = (double *) a; double *y = (double *) b; // return *x - *y; // this is WRONG... if (*x < *y) return -1; else if (*x > *y) return 1; return 0; } 

4. Comparison of key-based entries :

Sometimes you need to sort more complex things like recording. Here is the easiest way to do this using the qsort library.

 typedef struct { int key; double value; } the_record; int compare_function(const void *a,const void *b) { the_record *x = (the_record *) a; the_record *y = (the_record *) b; return x->key - y->key; } 
+52
Nov 24 '09 at 16:06
source share

Surely: qsort() is an implementation of the genus (not necessarily quicksort, as its name may seem).

Try man 3 qsort or read http://linux.die.net/man/3/qsort

+6
Nov 24 '09 at 5:32
source share

try qsort in stdlib.h.

+4
Nov 24 '09 at 5:31
source share

Use qsort () in stdlib.

@paxdiablo The qsort () function complies with ISO / IEC 9899: 1990 (`` ISO C90 '').

+3
Nov 24 '09 at 6:32
source share

Not yet in the standard library, https://github.com/swenson/sort has only two header files that you can include to access a wide range of incredibly fast sort sorts, for example:

 #define SORT_NAME int64
 #define SORT_TYPE int64_t
 #define SORT_CMP (x, y) ((x) - (y))
 #include "sort.h"
 / * You now have access to int64_quick_sort, int64_tim_sort, etc., eg, * /
 int64_quick_sort (arr, 128);  / * Assumes you have some int * arr or int arr [128];  * /

This should be at least twice as fast as the standard qsort library, since it does not use function pointers and has many other options for the sorting algorithm to choose from.

It is in C89, so basically every C compiler should work.

+3
May 04 '14 at 5:18
source share

There are several C sorting functions in stdlib.h . You can do man 3 qsort on a unix machine to get a list of them, but they include:

  • pyramidal sorting
  • quick sort
  • merger
+2
Nov 24 '09 at 5:31
source share



All Articles