c语言中使用自带的qsort(结构体排序)+ 快排
c中没有自带的sort函数emm
不过有自带的qsort函数
(其实用法都差不多(只是我经常以为c中有sort
头文件要用
1 #include <stdlib.h>
一定要重新把指针指向的值赋值给一个node类型,不然比较不了
1 struct node{ 2 int d,id,tmp; 3 }a[N]; 4 5 int cmp(const void *x,const void *y){ 6 struct node xx = *(struct node*)x; 7 struct node yy = *(struct node*)y; 8 return xx.d-yy.d; 9 } 10 11 qsort(a+1,n,sizeof(a[1]),cmp);//调用 排序a[1]~a[1+n]
这里贴一个代码,实现的功能是给定一个数组(数组中每个数不一样),然后输入一些数,问你这些数在数组中的位置(从0开始编号)
(其实是我看错题的产物(写都写了(就是实现一下离散化而已,当个小例子看吧
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 2*100010 6 7 struct node{ 8 int d,id,tmp; 9 }a[N]; 10