排序前:
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
排序前:
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
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;
}
如果问题得到解决,请点我回答左上角的采纳和向上的箭头,谢谢