• C语言快速排序函数------qsort();


    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    
    typedef struct in {
        int x;
        int y;
    }In;
    
    typedef struct char_ {
        char ch[100];
    }Char_;
    
    //对字符串排序函数
    int ptr_7(const void *a, const void *b) {
        return strcmp((*(Char_ *)a).ch, (*(Char_ *)b).ch);
    }
    
    //结构体一级排序函数
    int ptr_5(const void *a, const void *b) {
        return (*(In *)a).x>(*(In *)b).x ? 1 : -1;
    }
    
    //结构体二级排序
    int ptr_6(const void *a, const void *b) {
        In *c = (In *)a;
        In *d = (In *)b;
        if (c->x != d->x)return c->x - d->x;
        else
            return c->y - d->y;
    }
    
    //整数排序比较函数
    int ptr_1(const void *a, const void *b) {
        return *(int *)a - *(int *)b;
    }
    
    //double型排序比较函数
    int ptr_2(const void *a, const void *b) {
        return *(double *)a>*(double *)b ? 1 : -1;
    }
    //char型排序比较函数
    int ptr_3(const void *a, const void *b) {
        return *(char *)a - *(char *)b;
    }
    
    //对二维数组排序
    int ptr_4(const void *a, const void *b) {
        return ((int *)a)[0] - ((int *)b)[0];
    }
    
    
    
    int main() {
        int i, j, k, l;
        int a[5] = { 4,2,1,7,3 };
        double b[5] = { 3.21,4.35,5.34,86.3,12.4 };
        char c[5] = { 'g','t','a','v','p' };
        int d[3][2] = { { 5,4 },{ 1,6 },{ 9,0 } };
        qsort(a, 5, sizeof(a[0]), ptr_1);
        qsort(b, 5, sizeof(b[0]), ptr_2);
        qsort(c, 5, sizeof(c[0]), ptr_3);
        qsort(d, 3, sizeof(int) * 2, ptr_4);
        for (i = 0; i<5; i++) {
            printf("%d ", a[i]);
        }
        printf("
    ");
        for (i = 0; i<5; i++) {
            printf("%lf", b[i]);
        }
        printf("
    ");
        for (i = 0; i<5; i++) {
            printf("%c", c[i]);
        }
        for (i = 0; i<3; i++) {
            for (j = 0; j<2; j++) {
                printf("%d", d[i][j]);
            }
            printf("
    ");
    
        }
    
        In data[10];
        printf("输入结构体值");
        for (i = 0; i<10; i++) {
            scanf("%d%d", &data[i].x, &data[i].y);
        }
    
        qsort(data, 10, sizeof(data[0]), ptr_5);
        for (i = 0; i<10; i++) {
            printf("x=%d,y=%d
    ", data[i].x, data[i].y);
        }
        printf("----------------------------");
        qsort(data, 10, sizeof(data[0]), ptr_6);
        for (i = 0; i<10; i++) {
            printf("x=%d,y=%d
    ", data[i].x, data[i].y);
        }
    
    
        Char_ ch[5];
        for (i = 0; i<5; i++) {
            scanf("%s", ch[i].ch);
        }
        qsort(ch, 5, sizeof(ch[0]), ptr_7);
        for (i = 0; i<5; i++) {
            printf("%s
    ", ch[i].ch);
        }
        getch();
        return 0;
    }
  • 相关阅读:
    百度多图上传
    uploadify--上传文件控件
    JS获取时间
    CSS选择器
    派大星博客的美化之路
    百度地图--JS版
    css实现元素下出现横线动画
    盒模型显隐、定位与流式布局思想
    css进度条
    Build Sharepoint 2013 Farm
  • 原文地址:https://www.cnblogs.com/lin0/p/6640579.html
Copyright © 2020-2023  润新知