Tag: qsort

在C中使用qSort排序的数组字符串

问题很简单:有一些方法可以反过来返回返回“qsort”的有序数组,即我想避免使用任何辅助数组来使用qsort投资生成的数组。 这是我的代码,它从要排序的标准输入字符串中读取,并使用比较函数进行排序。 #include #include #include #include #include int cstring_cmp(const void *a, const void *b) { const char **ia = (const char **)a; const char **ib = (const char **)b; return strcasecmp(*ia, *ib); /* strcmp functions works exactly as expected from comparison function */ } 在此先感谢您的回复,对不起我的英语 int main (int argc, char *argv []) { int number; […]

为什么不需要将参数传递给qsort比较器函数?

以下代码取自此处 。 * qsort example */ #include #include int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) { return ( *(int*)a – *(int*)b ); } int main () { int n; qsort (values, 6, sizeof(int), compare); for (n=0; n<6; n++) printf ("%d ",values[n]); […]

用于qsort函数指针的Typecast

#include #include #include #include static int cmpstringp(const void *p1, const void *p2) { /* The actual arguments to this function are “pointers to pointers to char”, but strcmp(3) arguments are “pointers to char”, hence the following cast plus dereference */ return strcmp(* (char * const *) p1, * (char * const *) p2); } int […]

结构数组的qsort不起作用

我试图通过char对结构运行数组进行排序,但是当我打印数组时,没有任何内容被排序。 看看这个: 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_compare); for(i = […]

使用qsort对2D数组进行排序时发出警告

我正在尝试使用qsort在C中对2D数组进行排序。排序有效,但我收到警告: warning: initialization discards ‘const’ qualifier from pointer target type [enabled by default] 如何修改比较函数以消除警告(假设qsort需要参数const void *pa, const void *pb ? int cmp (const void *pa, const void *pb ) { const int (*a)[2] = pa; // warning here const int (*b)[2] = pb; // warning here if ( (*a)[1] (*b)[1] ) return -1; return 0; […]

qsort和bsearch指针数组

我需要排序一个指向struc的指针数组。 实际上,我需要在地址中进行搜索,以查看数组中是否存在指向结构的指定指针。 不幸的是,我在这些结构中没有任何“可比性”的东西,所以我只想按地址排序。 我的代码是这样的: item* arr[SIZE]; //something is inserted qsort(arr, SIZE, sizeof(item*), (void*)compare_funct); //CUT bsearch(curr, arr, SIZE, sizeof(item*), (void*)compare_funct); 我尝试创建一个compare_funct,只是将指针转换为int并返回它们的差异,但它似乎不起作用。 特别是,当我进行bsearch时,即使我知道元素包含在数组中,我总是得到一个NULL作为返回值。

C中的Quicksort实现?

我非常喜欢C中的qsort函数。它非常易于使用,并且允许我拖延学习C ++模板类型。 我有几个问题: 算法总是使用快速排序还是依赖于编译器实现? 您会建议使用此function还是模板真的有益处? 是否有任何我应该注意的事项以避免安全问题/段错误?

为什么这个qsort()不起作用?

我正在排序一个字符串数组(不区分大小写)。 qsort导致分段错误,可能是我的演员不合适。 #include #include #include int compare(const void *string1, const void *string2) { char *a = (char*)(string1); char *b = (char*)(string2); printf(“comparing %s AND %s\n”, a, b); return strcasecmp(a,b); } void sortListName(char **fileList, int noOfFiles) { printf(“Sorting\n”); qsort(fileList, noOfFiles, 260*sizeof(char), compare); return; } ** fileList =字符串数组(文件名) PS main()显而易见,工作正常。

C中每N个元素中最常见的

我有一个大的数组A,大小为[0,8388608]的“相对较小”的整数A [i] = [0,131072],我想找到每个N = 32个元素中最常出现的元素。 什么会更快, A.创建一个大小为131072的关联数组B,迭代32个元素,递增B [A [i]],然后迭代B,找到最大值,将B中的所有元素重置为0,重复| A | / 32次。 B. qsort每32个元素,找到A [i] == A [i-1]的最大范围(因此也是最常见的元素),重复| A | / 32次。 (编辑)C。别的。

stdlib qsort对结构的指针数组进行排序

我试图根据存储在我知道的“桶”结构的void *中存储的值对结构的指针数组(下面的定义)进行排序。 它编译并打印出我的数组桶及其值,没有任何错误或警告,但实际上并没有对数组进行排序。 我使用asserts试图找到任何可能导致qsort错误的地方。 结构定义: typedef struct _bucket{ void* val; char *word; }bucket; typedef struct _root{ bucket **list; int hashTableLength; }root; 要传递给qsort函数的Sort函数: int sortFunc(const void *a, const void *b){ bucket *bucketA=(bucket*)a; bucket *bucketB=(bucket*)b; int bucketAVal = *((int*)bucketA->val); int bucketBVal = *((int*)bucketB->val); assert((bucketAVal&&bucketBVal)!=0); return bucketAVal-bucketBVal; } 对数组进行排序并打印: void sort(root* inRoot, int(*sortFunc)(const void *a, const void *b)){ […]