Questions tagged [qsort]

qsort is the name of a C standard library function that sorts arrays using a caller-provided comparison function. Other programming languages may provide similar functions by the same name. Name notwithstanding, these functions do not necessarily implement quick-sort, so use the [quicksort] tag instead for questions specifically about the that algorithm. Use [qsort] ONLY for questions about library functions of that name.

qsort is the name of a C standard library function that sorts arrays of arbitrary element type using a caller-provided comparison function. Other programming languages may provide similar functions by the same name.

Although the name is suggestive of Hoare's "Quick Sort", these functions do not necessarily implement any variation on that algorithm. In particular, the prototypical example -- C's -- is not required by that language's standard to implement any specific algorithm.

On the other hand, qsort functions' specifications are typically consistent with implemention as Quick Sort. For example, they generally are not specified to be stable sorts, and C's in particular is not specified to be stable. (Standard Quick Sort is not stable.)

In practice, it would be surprising to encounter a qsort function without these properties common in common with Quick Sort implementations:

  • in-place sorting, with no more than O(log n) overhead in the average case.
  • performance bounded by O(n log n) in the average case, and no worse than O(n2) in the worst case.
585 questions
35
votes
3 answers

qsort: Cast the comparator function itself or the parameters in the body of comparator function?

There are a couple of obvious ways to use qsort: cast in the comparator: int cmp(const void *v1, const void *v2) { const double *d1 = v1, *d2 = v2; ⋮ } qsort(p, n, sizeof(double), cmp); or cast the comparator: int cmp(const double *d1,…
jjg
  • 686
  • 5
  • 16
26
votes
8 answers

How to qsort an array of pointers to char in C?

Suppose I have an array of pointers to char in C: char *data[5] = { "boda", "cydo", "washington", "dc", "obama" }; And I wish to sort this array using qsort: qsort(data, 5, sizeof(char *), compare_function); I am unable to come up with the compare…
bodacydo
  • 63,809
  • 83
  • 206
  • 303
17
votes
4 answers

Need help using qsort with an array of structs

Now, I have seen various examples, but I don't get what they mean. Here's my structure typedef struct profile{ char gender[1]; double soc; . . . } PROFILE; where soc is social security number that I'm going to be sorting by. I know…
Julian Hernandez
  • 189
  • 1
  • 1
  • 3
16
votes
3 answers

Stabilizing the standard library qsort?

I'm assuming that the good old qsort function in stdlib is not stable, because the man page doesn't say anything about it. This is the function I'm talking about: #include void qsort(void *base, size_t nmemb, size_t size, …
twk
  • 15,310
  • 21
  • 68
  • 95
15
votes
5 answers

Casting function pointers

I am writing a function that receives a pointer to a comparison function and an array of MyStructs and is supposed to sort the array according to the comparison function: void myStructSort( struct MyStruct *arr, …
Benjy Kessler
  • 6,552
  • 6
  • 32
  • 61
15
votes
1 answer

Trying to use qsort with vector

I'm trying to learn c++ and was trying using sort and qsort. sort() works just fine but qsort doesn't, I don't know why, so can you help me please this is the code I was trying to…
user1653150
  • 343
  • 1
  • 3
  • 15
14
votes
3 answers

Problem trying to use the C qsort function

#include #include float values[] = { 4, 1, 10, 9, 2, 5, -1, -9, -2,10000,-0.05,-3,-1.1 }; int compare (const void * a, const void * b) { return ( (int) (*(float*)a - *(float*)b) ); } int main () { int i; qsort…
Luiz Fernando
  • 151
  • 1
  • 1
  • 3
13
votes
4 answers

C qsort not working correctly

I don't know what I'm doing wrong but the following code does not sort the array properly. #include #include int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); } int main() { int x[] = {…
Ram
  • 1,151
  • 1
  • 10
  • 34
13
votes
3 answers

using qsort to sort an array of long long int not working for large nos

I am using this compare function to sort an array consisting of long long int nos. int compare(const void * p1,const void * p2) { return (* (long long int * )a-*(long long int * )b); } qsort(array,no of elements,sizeof(long long…
SHB
  • 559
  • 1
  • 6
  • 17
12
votes
2 answers

sorting 5d array in c

I am trying to figure out how to sort a multidimensional data (5 dimensions) in C. I know that using a 5d array is a solution that, from reading others posts on SO about this topic many folks find, if not altogether unethical, so aesthetically…
PaeneInsula
  • 1,906
  • 2
  • 28
  • 52
11
votes
9 answers

Is stdlib's qsort recursive?

I've read that qsort is just a generic sort, with no promises about implementation. I don't know about how libraries vary from platform to plaform, but assuming the Mac OS X and Linux implementations are broadly similar, are the qsort…
Joe
  • 42,600
  • 24
  • 134
  • 225
11
votes
2 answers

Is there a function similar to qsort() to be used in kernel space?

I'm writing a loadable kernel module and I need to use the function qsort() which apparently cannot be used in kernel space. Is there a function that I can use that has a similar functionality ? (Kernel version 3.5.0)
Varda Elentári
  • 1,964
  • 5
  • 33
  • 51
10
votes
4 answers

Qsort Comparison Function

I am a beginner to C and I am trying to understand the comparison function needed for the qsort function. Part One: Syntax A simple suggested use is this (I have included some main() code to print the results as well): #include #include…
Dlinet
  • 1,093
  • 3
  • 14
  • 22
9
votes
3 answers

Sorting an array of struct pointers using qsort

I'm getting weird results from trying to use qsort on this array of structs. I have this struct: struct access_data{ int sector; int arrival_time; int checked; int processed; }; I construct an array of access_data pointers from a…
wdonahoe
  • 901
  • 10
  • 19
9
votes
3 answers

Using qsort for character array in C

I'm trying to use qsort to sort a character array. I can't see why this is not working. I have a pointer to the compare function as the man pages specifies. Can someone please tell me what's wrong? Thanks. My code: #include #include…
user1527227
  • 1,546
  • 3
  • 21
  • 31
1
2 3
38 39