qsort

qsort of struct array not working

女生的网名这么多〃 提交于 2020-01-15 05:49:05
问题 I am trying to sort a struct run array called results by a char, but when I print the array, nothing is sorted. Have a look at this: struct run { char name[20], weekday[4], month[10]; (And some more...) }; typedef struct run run; int name_compare(const void *a, const void *b) { run *run1 = *(run **)a; run *run2 = *(run **)b; return strcmp(run1->name, run2->name); } int count_number_of_different_persons(run results[]) { int i = 0; qsort(results, sizeof(results) / sizeof(run), sizeof(run), name

二分法快速排序

风格不统一 提交于 2020-01-10 03:58:36
我在实现二分法快速排序的时候,最初的程序是这样的。 # include <iostream> using namespace std ; void qsort ( int arr [ ] , int left , int right ) { int l = left ; int r = right ; int value = arr [ l ] ; do { while ( l < r && arr [ r ] >= value ) r -- ; if ( arr [ r ] < value ) { arr [ l ] = arr [ r ] ; l ++ ; } while ( l < r && arr [ l ] <= value ) l ++ ; if ( arr [ l ] > value ) { arr [ r ] = arr [ l ] ; r -- ; } } while ( l != r ) ; arr [ l ] = value ; qsort ( arr , left , r -- ) ; qsort ( arr , l ++ , right ) ; } int a [ 100 ] ; int main ( ) { freopen ( "1.txt" , "r" , stdin ) ; freopen ( "2.txt" , "w" , stdout ) ;

Need help sorting an array of structures in C using qsort

天涯浪子 提交于 2020-01-05 05:45:08
问题 I have this struct. struct Transport { int id; float Price; }; Here I read the data into and array of structs. void read (struct Transport **Car, int *m) { int i; printf("Insert total number of cars: "); scanf("%d",m); *Car=(struct Transport*) malloc ((*m)*3*sizeof(struct Transport)); for(i=1; i<=*m; i++) { (*Car)[i].id=i; printf("Price: "); scanf("%f",&(*Car)[i].Price); printf("\n"); } } And here is the display function. void display(struct Transport *Car,int m) { int i; for(i=1; i<=m; i++)

How to qsort an array of pointers that use structs?

我是研究僧i 提交于 2019-12-31 02:41:20
问题 I want to sort an array of pointers by Id's. However qsort fails to work because of my lack of experience with pointers. typedef struct block{ int Id; char * name; } block; typedef struct { block ** data; int size_array; } database; if( ( database = malloc(sizeof(database)) ) == NULL ) { printf("Error: malloc failed\n"); exit(EXIT_FAILURE); } if( ( database->data = malloc( sizeof( block * ) * database->size_array ) ) == NULL ) { exit(EXIT_FAILURE); } for( i = 0; i < database->size_array; i++

Using qsort for character array in C

房东的猫 提交于 2019-12-30 06:15:28
问题 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 <stdio.h> #include <stdlib.h> #include <string.h> int cmpfunc( const void *a, const void *b) { return *(int*)a - *(int*)b; } void AlphabetSoup( char str[] ) { qsort(str, (size_t) strlen(str), (size_t) sizeof(char), cmpfunc); printf("%s\n", str); } int main() { char str1[]

QSorting a malloc'd array of structures?

喜欢而已 提交于 2019-12-25 11:53:26
问题 I have this comparator function for my qsort in C, but I seem to be getting a segmentation fault no matter what I try... int textCompare ( const void * a, const void * b ){ const char **x =(const char**)a; const char **y =(const char**)b; return strcmp(*x, *y); } Here is my qsort call: where message** mList = malloc(INITIAL_CAPACITY * sizeof(message)); and count is an integer keeping track of the last element. message is just a typedef struct that contains an int and a pointer to a char. I'm

qsort with array of structs?

回眸只為那壹抹淺笑 提交于 2019-12-25 11:50:52
问题 I am trying to use qsort on an array of structs but I get this error: expected primary-expression before '*' token struct muchie { int x,y,c; } a[100]; int cmp(const void* p, const void* q) { muchie vp,vq; vp=*(muchie* p); vq=*(muchie* q); return vp.c-vq.c; } // .... qsort(a,m,sizeof(muchie),cmp); 回答1: The casting of the parameters is wrong - should be *(muchie*)p instead of *(muchie* p) . Use: int cmp(const void* p, const void* q) { muchie vp,vq; vp=*(muchie*) p; vq=*(muchie*) q; return vp.c

cmpfunc in qsort() function in c

 ̄綄美尐妖づ 提交于 2019-12-25 03:45:27
问题 Can someone explain me cmpfunc which is used in the qsort function? What are a and b in this function and what are they pointing to? int cmpfunc(const void *a, const void *b) { return(*(int*)a - *(int*)b); } 回答1: a and b in cmpfunc are pointers to const void type. cmpfunc can accept pointer to elements of array of any data type. void * pointer can't be dereferenced, therefore a cast int * is needed before dereferencing. 回答2: In this inputs are *void and you need to comaper integers in your

How to sort an array of a structure using qsort?

蓝咒 提交于 2019-12-25 01:40:27
问题 A friend and I are trying to teach ourselves C and have decided to do, a what was first thought to be easy, an exercise, where we create a struct of two char containing 1. the forename and 2. the surname. A function read_person gets the users input, saves it in the structure and returns it. The input should be saved in a dynamically allocated array (all of which we have allegedly done correct so far). Then, using qsort, the array should be sorted ascendingly when it comes to the forename,

sorting 2-d array based on the first column

偶尔善良 提交于 2019-12-24 19:22:28
问题 I have a temp2.dat file that looks like this: 0.060493 1 0.5 1 1.596961 0 0.1 2 0.87758 1 0.3 1.5 0.165453 1 0 3 0.07085 1 0.3 4 0.125379 1 0.2 3 0.454202 1 0.2 2 0.373227 1 0.3 1 0.131486 1 0.3 3 0.867477 0 0.5 4 0.122609 0 0.8 9 Now I want to write the function in C to sort these 4 columns in an increasing order but only based on the values of the first column. I tried modifying the following code but it failed: struct data_t { double x; int y; double z; double k; }; int compare(const void