u010479604 2018-11-22 07:01 采纳率: 83.3%
浏览 816
已采纳

求用C语言利用快速排序法完成数组排序,完成如下例子,谢谢谢谢:

排序前:
1 1 1
1 1 3
2 1 3
1 2 1
2 1 2
1 2 2
2 2 2
1 2 3
2 2 1
1 1 2
1 2 4
1 3 1
2 1 1
2 2 3

排序号:
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 2 4
1 3 1
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3

  • 写回答

2条回答 默认 最新

  • threenewbee 2018-11-22 07:21
    关注

    C语言标准库的qsort函数,就是快速排序。

    #include "stdlib.h"
    #include "stdio.h"
    
    int data[14][3] = {
        { 1, 1, 1 },
        { 1, 1, 3 },
        { 2, 1, 3 },
        { 1, 2, 1 },
        { 2, 1, 2 },
        { 1, 2, 2 },
        { 2, 2, 2 },
        { 1, 2, 3 },
        { 2, 2, 1 },
        { 1, 1, 2 },
        { 1, 2, 4 },
        { 1, 3, 1 },
        { 2, 1, 1 },
        { 2, 2, 3 }
    };
    
    int order[14];
    
    int cmp(const void *a, const void *b)
    {
        int * x = data[(*(int *)a)];
        int * y = data[(*(int *)b)];
        if (x[0] == y[0])
        {
            if (x[1] == y[1])
                return x[2] - y[2];
            else
                return x[1] - y[1];
        }
        return x[0] - y[0];
    }
    
    int main()
    {
        printf("排序前\n");
        for (int i = 0; i < 14; i++)
        {
            order[i] = i;
            printf("%d %d %d\n", data[order[i]][0], data[order[i]][1], data[order[i]][2]);
        }
        qsort(order, 14, sizeof(int), cmp);
        printf("排序后\n");
        for (int i = 0; i < 14; i++)
        {
            printf("%d %d %d\n", data[order[i]][0], data[order[i]][1], data[order[i]][2]);
        }
        return 0;
    }
    
    
    

    图片说明

    如果问题得到解决,请点我回答左上角的采纳和向上的箭头,谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记