C - 排序二维整数数组

标签 c arrays sorting

我有一个二维数组,其中第一行是一个 id,第二行是出现的次数。我想按出现次数对数组进行排序(id 随数字一起移动) 我有以下代码(最小可行示例):

#include <stdio.h>
#include <stdlib.h>

int main(){
    int colors[64][2];
    int r;int a;int b;int i;
    for (i = 0; i < 63; ++i){
        r=rand()%40;
        colors[i][0]=i;
        colors[i][1]=r;
    }
    for (i = 0; i < 62; ++i){
        printf("%d : ", colors[i][0]);
        printf("%d\n", colors[i][1]);
    }
    printf("--------------------------------------------------------------\n");
    //sort colors now
    r=1;
    while(r==1){
        for (i = 0; i < 63; ++i){
            r=0;
            if (colors[i][1] < colors[i+1][1]){
                a = colors[i][0];
                b = colors[i][1];
                colors[i][0] = colors[i+1][0];
                colors[i][1] = colors[i+1][1];
                colors[i+1][0] = a;
                colors[i+1][1] = b;
                r=1;
            }
        }
    }
    for (i = 0; i < 62; ++i){
        printf("%d : ", colors[i][0]);
        printf("%d\n", colors[i][1]);
    }
}

但是,除了 id=1 的值已在 9 和 10 之间移动之外,输出没有进行任何排序。 当前输出:

0 : 23
2 : 17
3 : 35
4 : 33
5 : 15
6 : 26
7 : 12
8 : 9
9 : 21
1 : 6
11 : 27
12 : 10
13 : 19
14 : 3
15 : 6
16 : 20
17 : 26
18 : 12
19 : 16
20 : 11
21 : 8
22 : 7
23 : 29
24 : 22
25 : 10
26 : 22
27 : 3
28 : 27
29 : 15
30 : 9
10 : 2
32 : 22
33 : 18
34 : 29
35 : 7
36 : 33
37 : 16
38 : 11
31 : 2
40 : 29
41 : 13
42 : 21
43 : 39
44 : 24
45 : 17
46 : 38
47 : 4
48 : 35
49 : 10
50 : 13
51 : 6
52 : 11
53 : 20
54 : 36
55 : 33
56 : 22
57 : 10
58 : 36
39 : 2
60 : 25
61 : 5
62 : 4

非常感谢所有帮助,谢谢

最佳答案

程序

#include <stdlib.h>                                                                                                                                                                                                                                                          
#include <stdio.h>                                                                                                                                                                                                                                                           

#define ARRAY_SIZE(_a_) (sizeof(_a_) / sizeof(_a_[0]))                                                                                                                                                                                                                       

struct ColorPrevalence                                                                                                                                                                                                                                                       
{                                                                                                                                                                                                                                                                            
    unsigned int color_id;                                                                                                                                                                                                                                                   
    unsigned int count;                                                                                                                                                                                                                                                      
} sample_data[] =                                                                                                                                                                                                                                                            
{                                                                                                                                                                                                                                                                            
    {10, 3},                                                                                                                                                                                                                                                                 
    {4, 17},                                                                                                                                                                                                                                                                 
    {19, 6},                                                                                                                                                                                                                                                                 
    {7, 3},                                                                                                                                                                                                                                                                  
    {3, 0},                                                                                                                                                                                                                                                                  
    {2, 1},                                                                                                                                                                                                                                                                  
};                                                                                                                                                                                                                                                                           

void print_data(void)                                                                                                                                                                                                                                                        
{                                                                                                                                                                                                                                                                            
    for (size_t i = 0; i < ARRAY_SIZE(sample_data); i++)                                                                                                                                                                                                                     
    {                                                                                                                                                                                                                                                                        
        printf(                                                                                                                                                                                                                                                              
            "{color: %u, count: %u}\n",                                                                                                                                                                                                                                      
            sample_data[i].color_id,                                                                                                                                                                                                                                         
            sample_data[i].count);                                                                                                                                                                                                                                           
    }                                                                                                                                                                                                                                                                        
}                                                                                                                                                                                                                                                                            

int compare_ColorPrevalence(const void *v1, const void *v2)                                                                                                                                                                                                                  
{                                                                                                                                                                                                                                                                            
    const struct ColorPrevalence *c1 = v1;                                                                                                                                                                                                                                   
    const struct ColorPrevalence *c2 = v2;                                                                                                                                                                                                                                   
    return c1->count - c2->count;                                                                                                                                                                                                                                            
}                                                                                                                                                                                                                                                                            

int main(int argc, char **argv)                                                                                                                                                                                                                                              
{                                                                                                                                                                                                                                                                            
    puts("Before Sorting:");                                                                                                                                                                                                                                                 
    print_data();                                                                                                                                                                                                                                                            
    qsort(                                                                                                                                                                                                                                                                   
        sample_data,                                                                                                                                                                                                                                                         
        ARRAY_SIZE(sample_data),                                                                                                                                                                                                                                             
        sizeof(struct ColorPrevalence),                                                                                                                                                                                                                                      
        compare_ColorPrevalence);                                                                                                                                                                                                                                            
    puts("After Sorting:");                                                                                                                                                                                                                                                  
    print_data();                                                                                                                                                                                                                                                            
}                        

输出

$ ./qsort 
Before Sorting:
{color: 10, count: 3}
{color: 4, count: 17}
{color: 19, count: 6}
{color: 7, count: 3}
{color: 3, count: 0}
{color: 2, count: 1}
After Sorting:
{color: 3, count: 0}
{color: 2, count: 1}
{color: 10, count: 3}
{color: 7, count: 3}
{color: 19, count: 6}
{color: 4, count: 17}

关于C - 排序二维整数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41729517/

相关文章:

c - 如何让Ctrl-c结束程序

c - 将 scanf 与 char 数组一起使用时遇到问题

java - java中new String[]{}和new String[]的区别

ios - 按日期对 NSMutableArray 中的不同实体进行排序

java - 根据数字键的顺序在 map 中排序

java - 使用 Glazed Lists 和 JXTable 进行表外列排序

c - AVL树中平衡因子的重新计算

c - 在 C 中将文本添加到字符串的末尾

将多个 C 代码编译为一个汇编文件

java - 通过数组传递不确定数量的 Int