• qsort


     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 
     5 
     6 int a[10]={1,9,5,6,7,4,1,5,8,3};
     7 typedef struct
     8 {
     9     double x;
    10     double y;
    11 }b;
    12 //一维数组的排序(整形)
    13 int comp(const void *p,const void *q)
    14 {
    15     return *(int *)p>*(int *)q ? 1:-1;//从小到大
    16 
    17     /*   return  -(*(int *)p-*(int *)q);//从大到小
    18            return *(int *)p>*(int *)q ? 1:-1;//从小到大
    19     */
    20 }
    21 //一维数组的排序(double型的要注意了)
    22 
    23 
    24 double in[100]; 
    25 
    26 int cmp( const void *a , const void *b ) 
    27 { 
    28     return *(double *)a > *(double *)b ? 1 : -1; //直接返回的话可能会有浮点误差,造成错误的判断
    29 }
    30 
    31 int cmp1(const void *p,const void *q)
    32 {
    33     return (*(b*)p).x>(*(b*)q).x?1:-1;
    34 
    35 }
    36 //按照x从小到大排序,当x相等时按照y从大到小排序
    37 int cmp2(const void *p,const void *q)
    38 {
    39     b *c=(b*)p;
    40     b *d=(b*)q;
    41     if(c->x!=d->x)  return c->x>d->x?1:-1;
    42     else return d->y>c->y?1:-1;
    43 }
    44 
    45 
    46 int cmp3(const void *p,const void *q)
    47 {
    48         return strcmp((*(Node *)p).str,(*(Node *)q).str);
    49 }
    50 
    51 int main()
    52 {
    53     int i;
    54     /*qsort(a,10,sizeof(int),comp);
    55     for(i=0;i<10;i++)
    56         printf("%d ",a[i]);*/
    57     b t[5];
    58     t[0].x=0;
    59     t[0].y=4;
    60     t[1].x=0;
    61     t[1].y=0;
    62     t[2].x=5;
    63     t[2].y=8;
    64     t[3].x=5;
    65     t[3].y=1;
    66     t[4].x=2;
    67     t[4].y=7;
    68     qsort(t,5,sizeof(b),cmp2);
    69     for(i=0;i<5;i++)
    70         printf("%.0lf %.0lf\n",t[i].x,t[i].y)    ;
    71     
    72 
    73     return 0;
    74 }
  • 相关阅读:
    oracle的nvl函数的用法
    简单实用的MD5加密算法
    oracle触发器使用笔记
    Html学习
    连接字符串
    oracle触发器使用笔记2
    oracle中如何给有空值的数据排序
    Zend Frame 添加Smarty模板引擎
    HDU 2464 A Pair of Graph
    POJ 1466 Girls and Boys
  • 原文地址:https://www.cnblogs.com/xiaofanke/p/2818941.html
Copyright © 2020-2023  润新知