qsort

Qsorting 2d pointer arrays

╄→гoц情女王★ 提交于 2019-12-24 15:42:55
问题 I'm trying to sort a 2d array of pointers using qsort. The only issue I have right now is originally I was using statically declared arrays now switching over to pointers. I'm almost tempted to switch to structs but being stubborn that I can't get this to work. So far I malloc the 2d array of pointers[array2d[m][3] was the intended size]: int **array2d; array2d = (int**)malloc((m)*sizeof(int*)); for(i=0; i<=m; i++) array2d = [i]=(int*)malloc(3*sizeof(int)); qsort(array2d, m, 3*sizeof(int**)

Sorting a 2D array with qsort

时光怂恿深爱的人放手 提交于 2019-12-24 14:28:25
问题 I'm trying to sort 2d array. First i sort it by column, then by rows. Column by column is working but row by row not. What's wrong in this code? int scmpr (const void *a, const void *b){ return strcmp((const char*)a, (const char*)b); } int main(void){ int i,j; char **tab; tab=(char**)malloc(sizeof(char*)* 10); for(i=0; i<10; i++){ tab[i]=(char*)malloc(sizeof(char)*15); } for(i=0; i<10; i++){ for(j=0; j<15; j++){ tab[i][j]=rand()%20+'b'; printf("%c ", tab[i][j]); } puts(""); } for (i = 0; i<10

My Qsort comparison function causes strange stuff in memory

℡╲_俬逩灬. 提交于 2019-12-24 12:24:19
问题 To get used to dynamically creating two dimensional arrays, I wanted to create one that could be sorted by the fifth member in each array. The array is a multidimensional array of groups of doubles. Each group holds five doubles. The last two doubles are calculated based on the previous 3 by some random expression I wrote. There are as many arrays as there are groups of doubles it reads from a file. When I wrote it, I got very random results from the sort. #include <stdio.h> #include <stdlib

Writing a compare function for a structure for qsort?

半世苍凉 提交于 2019-12-24 03:46:07
问题 I'm having trouble writing a compare function for qsort function in C. This is what I currently have: int cmpfunc(const void *a, const void *b) { return (*(Individual*)a->fitness - *(Individual*)b->fitness); } I know how the compare function works but I don't understand how to reference an integer value within my structure called Individual . Here is the structure of the Individual. typedef struct { PPM_IMAGE image; double fitness; } Individual; I want to compare the fitness values within the

Quicksort implementation in C?

纵饮孤独 提交于 2019-12-23 15:37:26
问题 I really like the qsort function in C. It's so easy to use and allows me to procrastinate learning C++ template types. I have a few questions about this: Is the algorithm used always a quicksort or is it compiler-implementation-dependent? Would you recommend using this function or is there a real benefit to templates? Are there any things I should watch out for to avoid security problems/segfaults? 回答1: Is the algorithm used always a quicksort or is it compiler-implementation-dependent? It is

How to compare long doubles with qsort and with regard to NaN?

最后都变了- 提交于 2019-12-21 12:56:25
问题 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 array. qsort() imposes some restriction on the compare function. The function shall return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. C11dr §7.22.5.2 3 When the same objects ... are passed

Bug in quicksort example (K&R C book)?

一个人想着一个人 提交于 2019-12-21 04:34:06
问题 This quicksort is supposed to sort "v[left]...v[right] into increasing order"; copied (without comments) from The C Programming Language by K&R (Second Edition): void qsort(int v[], int left, int right) { int i, last; void swap(int v[], int i, int j); if (left >= right) return; swap(v, left, (left + right) / 2); last = left; for (i = left+1; i <= right; i++) if (v[i] < v[left]) swap(v, ++last, i); swap(v, left, last); qsort(v, left, last-1); qsort(v, last+1, right); } I think there's a bug at

How to use qsort for an array of strings?

我们两清 提交于 2019-12-19 10:26:29
问题 #include <stdio.h> #include <string.h> #include <stdlib.h> int sortstring(const void *str1, const void *str2) { const char *rec1 = str1; const char *rec2 = str2; } void sortutil(char* lines[]) { qsort(lines, 200, sizeof(char), sortstring); } #include <stdio.h> #include <string.h> #include <stdlib.h> #include "sortutil.h" int getarray(char *lines[]) { int i = 0; char *text = (char *)malloc(200); while (fgets(text, 200, stdin) != NULL) { lines[i] = text; i++; text = (char *)malloc(200); }

keeping track of the original indices of an array after sorting in C

为君一笑 提交于 2019-12-18 09:06:08
问题 I have an array let's say A[5] , the 5 elements are 5,4,1,2,3 . Now I sort these arrays in ascending order. so the resulting array will now be 1,2,3,4,5 . I use qsort() function of stdlib.h to sort this. The question is how can I get the indices of the original array with respect to my new array. originally my indices were 0,1,2,3,4 for corresponding values of 5,4,1,2,3 and now the indices have changed to 2,3,4,1,0. How can I get these indices efficiently in C? Thank you in advance(please

Sorting an array of struct pointers using qsort

好久不见. 提交于 2019-12-17 18:36:42
问题 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 file such that they are sorted by arrival_time, but I need to sort them by sector later, so I have the following: int compare_data(const void* a, const void* b){ if (((access_data*)a)->sector < ((access_data*)b)->sector) return 1; else if (((access_data*)a)->sector >