qsort Questions

1

Overview There are a few questions similar to this one but they are all slightly different. To be clear, if values is an array of integers, I want to find perm such that sorted_values (values sort...
Antilles asked 15/2, 2018 at 9:42

9

Solved

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 *), comp...
Realize asked 15/8, 2010 at 20:39

4

Solved

I have a report, unconfirmed by me but from a reliable source, that the code qsort(a, n, sizeof *a, cmpfunc); is compiled by a modern version of gcc as if it had been written if(n == 0) __b...
Gandhi asked 26/10, 2022 at 12:29

2

Solved

I was just playing around with sorting in golang and I found a qsort function on stackoverflow. It seems to run about twice as fast as the native sort function in golang. I've tried it with differe...
Pooler asked 24/4, 2014 at 17:59

2

I'm doing an analysis for the quicksort (qsort from c++ STL) algorithm, the code is: #include <iostream> #include <fstream> #include <ctime> #include <bits/stdc++.h> #includ...
Sandrasandro asked 16/3, 2021 at 17:34

3

Solved

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 cas...
Chev asked 27/2, 2021 at 15:59

6

Solved

First, I defined a dynamic array with 2 columns and 10 row. The integer number is set to 10 here just for example. int** array; int number = 10; array = malloc(number * sizeof(int*)); for (i = 0...
Spiegelman asked 19/6, 2013 at 22:4

5

Solved

According to this site, I have done the following program which sorts strings. #include <cstdlib> #include <stdio.h> #include <stdlib.h> #include <string.h> char list[5][4]=...
Hulett asked 21/9, 2010 at 6:47

4

Solved

I don't know what I'm doing wrong but the following code does not sort the array properly. #include <stdio.h> #include <stdlib.h> int compare(const void* a, const void* b) { return (...
Trout asked 23/5, 2011 at 22:12

3

#include <stdio.h> #include <stdlib.h> 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*...
Merilyn asked 7/10, 2010 at 22:47

5

Solved

I see lots of people use subtraction in a qsort comparator function. I think it is wrong because when dealing with these numbers: int nums[]={-2147483648,1,2,3}; INT_MIN = -2147483648; int compare...
Scop asked 5/4, 2018 at 3:37

3

Solved

I'm not sure if this is possible to do with qsort because what I want to sort (array of pointers to struct) is not what I am comparing (strings). Here is an abridged version of my program (assume ...
Shiloh asked 4/5, 2012 at 5:27

2

Solved

How to compare long doubles with qsort() and with regard to not-a-number? When sorting an array that might contain not-a-numbers, I would like to put all the those NAN to one end of the sorted ...
Vestibule asked 2/1, 2018 at 23:48

3

Solved

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 <stdlib.h> void ...
Rancorous asked 25/2, 2009 at 4:6

3

Solved

I am making C dynamic array library, kind of. Note that I'm doing it for fun in my free time, so please do not recommend million of existing libraries. I started implementing sorting. The array is...
Lifesaving asked 28/11, 2016 at 23:36

4

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 numbe...
Loader asked 24/5, 2011 at 3:55

3

Assume I have a square matrix A of size n, defined as a std::vector<double>. std::vector<double> A(n*n); The elements of the matrix are accessed the usual way: double a_ij = A[i*n +...
Nannettenanni asked 3/11, 2016 at 9:31

4

Solved

I have an array which looks like this: int array[] = {4.53, 3.65, 7.43, 9.54, 0.72, 0.0} I am just wondering what method I can use to partially sort this array to bring the top three biggest dou...
Whipcord asked 15/10, 2016 at 17:15

2

Solved

When I use qsort() in the C on my Mac, these code works well, It can sort every lines in one file well. int compare(const void *p, const void *q) { return strcmp(p,q); } void function_name(){ c...
Pelvic asked 7/9, 2016 at 2:29

2

I am trying to sort my QList based on a QDateTime but I get the following error: must use '.*' or '->*' to call pointer-to-member function in 'lessThan (...)', e.g. '(... ->* lessThan) (...)...
Scintilla asked 25/11, 2015 at 9:52

5

Solved

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( stru...
Assignat asked 11/8, 2015 at 13:14

5

I wrote a (qsort-compatible) comparison function for a struct that has some unsigned fields in it: typedef struct { int a; unsigned b; } T; int cmp(T t1, T t2) { // Decreasing order in "a" if...
Oeildeboeuf asked 9/6, 2015 at 15:11

4

Solved

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 prin...
Donoghue asked 27/12, 2012 at 18:36

6

Solved

I found this sample code online, which explains how the qsort function works. I could not understand what the compare function returns. #include "stdlib.h" int values[] = { 88, 56, 100, 2, 25 }; ...
Soni asked 4/12, 2014 at 0:23

3

Solved

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(arra...
Behemoth asked 14/7, 2013 at 10:19

© 2022 - 2024 — McMap. All rights reserved.