为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

sort函数的用法(The use of sort functions)

2017-09-01 6页 doc 21KB 42阅读

用户头像

is_531654

暂无简介

举报
sort函数的用法(The use of sort functions)sort函数的用法(The use of sort functions) sort函数的用法(The use of sort functions) The use of sort functions When you do ACM questions, sorting is often a required operation. If every time I write a bubble sort of O (n^2) sorting, not only the program is easy to overtime,...
sort函数的用法(The use of sort functions)
sort函数的用法(The use of sort functions) sort函数的用法(The use of sort functions) The use of sort functions When you do ACM questions, sorting is often a required operation. If every time I write a bubble sort of O (n^2) sorting, not only the program is easy to overtime, but also wasting valuable game time, it is likely to make mistakes. STL, there is a sort function, you can directly sort the array, the complexity is n*log2 (n). To use this function, you need to include the header file. This function can pass two arguments or three arguments. The first parameter is the first address of the interval to be sorted, and the next address where the second parameter is the range end address. That is, the sorting interval is [a, B). In simple terms, there is an array of int a[100], which you want to sort the elements from a[0] to a[99], just write sort (a, a+100), and the default sort is ascending. Take my "AC's policy" question, which requires the sorting of zeroth to len-1 elements in the logarithm group T, and sort (T, t+len); The same is true for vector V, sort (v.begin (), v.end ()); Sorted data types are not limited to integers, as long as they are defined by types less than operations, such as string classes string. If there is no data type that is smaller than the calculation, or if you want to change the order of the sorting, you need the third parameter -- the comparison function. A comparison function is a function that is defined by itself. The return value is bool, which specifies what relationship is less than". You can define a comparison function CMP in order to put the array of integers in descending order Bool CMP (int, a, int, b) { Return a>b; } When sorting, write sort (a, a+100, CMP); Suppose you have defined a structure, node Struct node{ Int a; Int b; Double c; } There is an array of node type node arr[100], which you want to sort: first by ascending the a value, and if the a value is the same, then put the b values in descending order, and if B is the same, rank them in descending order of C. You can write such a comparison function: Here is the code snippet: Bool CMP (node, x, node, y) { If (X.A, =y.a) return x.ay.b; Return return x.c>y.c; } when sorting, write sort (arr, arr+100, CMP); Qsort (s[0], N, sizeof (s[0]), CMP); Int CMP (const, void, *a, const, void, *b) { Return * (int *) a-* (int *) b; } First, sort the int type array Int num[100]; Sample: Int CMP (const, void, *a, const, void, *b) { Return * (int *) a * (int *) b; } Qsort (Num, 100, sizeof (num[0]), CMP); Two, sort the char type array (with the int type) Char word[100]; Sample: Int CMP (const, void, *a, const, void, *b) { Return * (char *) a * (int *) b; } Qsort (word, 100, sizeof (word[0]), CMP); Three, sorting double type arrays (with special attention) Double in[100]; Int CMP (const, void, *a, const, void, *b) { Return * (double *) a > * (double *) B, 1: -1; } Qsort (in, 100, sizeof (in[0]), CMP); Four, sort the structure at the first level Struct In { Double data; Int other; }s[100] According to the value of data / / from general structure sort, sort type data on the key data structures in the body can be a reference to the above example write Int CMP (const, void, *a, const, void, *b) { Return ((In *) a) ->data - - ((In *) b) ->data; } Qsort (s, 100, sizeof (s[0]), CMP); Five pairs of structures 结构在 { int x; y; } [ 100 ]; / /按照X从小到大排序,当相等时按照从大到小排序X Y int cmp(const void *,const void * B) { in in in(*)a; in in in(in *)b中的; 如果(c x)~=返回); 否则返回; } qsort(s,100,sizeof(s [ 0 ]),CMP); 六、对字符串进行排序 结构在 { int数据; 字符STR [ 100 ]; } [ 100 ]; / /按照结构体中字符串STR的字典顺序排序 int cmp(const void *,const void * B) { 返回strcmp(((*)a)-> str((*)b)-> STR); } qsort(s,100,sizeof(s [ 0 ]),CMP); 七、计算几何中求凸包的CMP int cmp(const void *,const void * B)/重点CMP函数,把除了1点外的所有点,旋转角度排序 { 点(=); 点(=); 如果(钙(* C * D、P [ 1 ])< 0)返回1; 否则如果(~钙(* C * D、P [ 1 ])和DIS(C?X,C -> Y、P [ 1 ]。X、P [ 1 ]。Y),DIS(D - > X,D -> Y、P [ 1 ] [ 1,X,P ]。Y))/如果在一条直线上,则把远的放在前面 返回1; 否则返回- 1; }
/
本文档为【sort函数的用法(The use of sort functions)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索