首页 > 随笔档案 > 桶排序-C-结构体排序

桶排序-C-结构体排序

Publish:

struct TS {
        int index;
        char name[10];
    };
    
    struct TS b[5] = {
        {5,"s5"},
        {2,"s2"},
        {3,"s3"},
        {1,"s1"},
        {8,"s8"}
    };
    
    struct TS a[11];
    int i;
    int length = sizeof(a) / sizeof (struct TS);
    for(i=0;i<length;i++){
        struct TS tmp = {0,""};
        a[i] = tmp;
    }
    
    for (i=0; i<5; i++) {
        int num = b[i].index;
        if(a[num].index >= 0){
            a[num] = b[i];
        }else{
            struct TS tmp = {0,""};
            a[num] = tmp;
        }
    }
    
    for (i=0; i<11; i++) {
        if(a[i].index)
            printf("%s\n",a[i].name);
    }

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: levy