rer*_*run 111
qsort()是你正在寻找的功能.您可以使用指向数据数组的指针,该数组中的元素数,每个元素的大小以及比较函数来调用它.
它发挥了它的魔力,你的阵列就地排序.一个例子如下:
#include <stdio.h>
#include <stdlib.h>
int comp (const void * elem1, const void * elem2)
{
int f = *((int*)elem1);
int s = *((int*)elem2);
if (f > s) return 1;
if (f < s) return -1;
return 0;
}
int main(int argc, char* argv[])
{
int x[] = {4,5,2,3,1,0,9,8,6,7};
qsort (x, sizeof(x)/sizeof(*x), sizeof(*x), comp);
for (int i = 0 ; i < 10 ; i++)
printf ("%d ", x[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
wha*_*cko 57
C/C++标准库<stdlib.h>包含qsort函数.
这不是世界上最好的快速排序实现,但它足够快且非常容易使用... qsort的正式语法是:
qsort(<arrayname>,<size>,sizeof(<elementsize>),compare_function);
Run Code Online (Sandbox Code Playgroud)
您需要实现的唯一事情是compare_function,它接受两个类型为"const void"的参数,可以转换为适当的数据结构,然后返回以下三个值中的一个:
1.比较整数列表:
可简单地把A和B为整数,如果x < y,x-y是负的,x == y,x-y = 0,x > y,x-y是积极
x-y的捷径办法做到这一点:)逆转*x - *y,以*y - *x在减少排序/倒序
int compare_function(const void *a,const void *b) {
int *x = (int *) a;
int *y = (int *) b;
return *x - *y;
}
Run Code Online (Sandbox Code Playgroud)
2.比较字符串列表:
为了比较字符串,你需要lib strcmp里面的函数<string.h>.
strcmp默认情况下会返回-ve,0,ve恰当...以相反顺序排序,只需反转strcmp返回的符号
#include <string.h>
int compare_function(const void *a,const void *b) {
return (strcmp((char *)a,(char *)b));
}
Run Code Online (Sandbox Code Playgroud)
3.比较浮点数:
int compare_function(const void *a,const void *b) {
double *x = (double *) a;
double *y = (double *) b;
// return *x - *y; // this is WRONG...
if (*x < *y) return -1;
else if (*x > *y) return 1; return 0;
}
Run Code Online (Sandbox Code Playgroud)
4.根据密钥比较记录:
有时您需要对更复杂的东西进行排序,例如记录.这是使用qsort库来完成它的最简单方法.
typedef struct {
int key;
double value;
} the_record;
int compare_function(const void *a,const void *b) {
the_record *x = (the_record *) a;
the_record *y = (the_record *) b;
return x->key - y->key;
}
Run Code Online (Sandbox Code Playgroud)
当然: qsort()是一种排序的实现(不一定是名称可能暗示的快速排序).
尝试man 3 qsort或阅读http://linux.die.net/man/3/qsort
小智 5
虽然不是完全在标准库中,但https://github.com/swenson/sort仅有两个头文件,您可以包括这些头文件来访问各种令人难以置信的快速排序路由,例如:
#define SORT_NAME int64 #define SORT_TYPE int64_t #define SORT_CMP(x, y) ((x) - (y)) #include "sort.h" /* You now have access to int64_quick_sort, int64_tim_sort, etc., e.g., */ int64_quick_sort(arr, 128); /* Assumes you have some int *arr or int arr[128]; */
这应该至少是标准库的两倍qsort,因为它不使用函数指针,并且有许多其他排序算法选项可供选择。
它在C89中,因此基本上应该在每个C编译器中都可以工作。
我想你正在寻找qsort。函数是在中
qsort找到的快速排序算法的实现。stdlib.hC/C++
这是调用函数的语法qsort:
void qsort(void *base, size_t nmemb, size_t size,int (*compar)(const void *, const void *));
Run Code Online (Sandbox Code Playgroud)
参数列表:
base:指向数组第一个元素或基地址的指针
nmemb:数组中的元素数量
size:每个元素的大小(以字节为单位)
compar:比较两个元素的函数
这是一个用于qsort对数组进行排序的代码示例:
#include <stdio.h>
#include <stdlib.h>
int arr[] = { 33, 12, 6, 2, 76 };
// compare function, compares two elements
int compare (const void * num1, const void * num2) {
if(*(int*)num1 > *(int*)num2)
return 1;
else
return -1;
}
int main () {
int i;
printf("Before sorting the array: \n");
for( i = 0 ; i < 5; i++ ) {
printf("%d ", arr[i]);
}
// calling qsort
qsort(arr, 5, sizeof(int), compare);
printf("\nAfter sorting the array: \n");
for( i = 0 ; i < 5; i++ ) {
printf("%d ", arr[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您可以man 3 qsort在 Linux/Mac 终端中输入以获取有关qsort.
链接到 qsort 手册页
中提供了多种 C 排序函数stdlib.h。您可以man 3 qsort在 UNIX 机器上获取它们的列表,但它们包括: