c 快速排序不工作
学习C,这是一个练习。
要求用户输入二维数组的维度。 然后用户输入该数组的值。 最后,程序必须能够将每一行从最低到最高排序并打印结果。
例如
输入:
2 2
13 11
13 11
应该打印:
11 13
11 13
但我的程序正在打印:
11 13
13 11
如果在我的代码中我声明了一个固定的数组大小,比如说矩阵 [2] [2] 并将所有代码更改为该数组维度,则该程序运行良好,但如果我声明矩阵[MAX][MAX],因为我不知道数组的大小是多少,所以它给了我上面发布的输出。
这是因为我没有使用指针和内存分配吗? (记住我还在学习C)。
这是我完成的代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
/*Print the final two-dimensional array*/
void print( int a[MAX][MAX], int b[])
{
int i,j;
for (i=0; i<b[0]; i++)
{
for (j=0; j<b[1]; j++)
{
printf("%d", a[i][j]);
if (j < b[1]-1)
{
printf(" ");
}
}
printf("\n");
}
}
/* Sort my array */
int Qsort (int v[MAX][MAX], int li, int ls)
{
int j;
if (li < ls)
{
j = order(v,li, ls);
Qsort (v, li, j-1);
Qsort (v, j+1, ls);
}
return v[MAX][MAX];
}
int order(int x[], int li, int ls)
{
int a, down, up, temp;
a=x[li]; down=li; up=ls;
while (down<up)
{
while (x[down]<=a && down<ls)
down ++;
while (x[up]>a)
up --;
if (down < up)
{
temp=x[down];
x[down]=x[up];
x[up]=temp;
}
}
x[li]=x[up];
x[up]= a;
return up;
}
int main()
{
int i,j,size,jump,jump2;
int tam[2];
int matrix[MAX][MAX];
/*Define rows and columms*/
i=0;
while (i<2)
{
scanf("%d", &tam[i]);
i++;
}
/*Fill the array with the numbers the user inputs*/
for (i=0; i<tam[0]; i++)
{
for (j=0; j<tam[1]; j++)
{
scanf("%d", &matrix[i][j]);
}
}
size = tam[1];
jump = 1;
jump2 = 0;
for(i=0;i<tam[0];i++)
{
Qsort (matrix,jump2, jump*size-1);
jump++;
jump2 = jump2 + size;
}
print (matrix,tam);
return 0;
}
谢谢,
Favolas
Learning C and this is an exercise.
The user is asked to input the dimension of an two-dimensional array.
Then the user inputs the values of that array.
Finally, the program must be able to sort every line from the lowest to the greatest and print the result.
For instance
input:
2 2
13 11
13 11
Should print:
11 13
11 13
But my program is printing:
11 13
13 11
If in my code I declare a fixed array size, lets say matrix[2][2] and change all the code to that array dimension, the program works great but if I declare matrix[MAX][MAX], since I don know what would be the size of the array, it gives me the output I've posted above.
Is this because I'm not using pointers and memory allocation? (Remember that I'm still learning C).
Here is the code I've done:
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
/*Print the final two-dimensional array*/
void print( int a[MAX][MAX], int b[])
{
int i,j;
for (i=0; i<b[0]; i++)
{
for (j=0; j<b[1]; j++)
{
printf("%d", a[i][j]);
if (j < b[1]-1)
{
printf(" ");
}
}
printf("\n");
}
}
/* Sort my array */
int Qsort (int v[MAX][MAX], int li, int ls)
{
int j;
if (li < ls)
{
j = order(v,li, ls);
Qsort (v, li, j-1);
Qsort (v, j+1, ls);
}
return v[MAX][MAX];
}
int order(int x[], int li, int ls)
{
int a, down, up, temp;
a=x[li]; down=li; up=ls;
while (down<up)
{
while (x[down]<=a && down<ls)
down ++;
while (x[up]>a)
up --;
if (down < up)
{
temp=x[down];
x[down]=x[up];
x[up]=temp;
}
}
x[li]=x[up];
x[up]= a;
return up;
}
int main()
{
int i,j,size,jump,jump2;
int tam[2];
int matrix[MAX][MAX];
/*Define rows and columms*/
i=0;
while (i<2)
{
scanf("%d", &tam[i]);
i++;
}
/*Fill the array with the numbers the user inputs*/
for (i=0; i<tam[0]; i++)
{
for (j=0; j<tam[1]; j++)
{
scanf("%d", &matrix[i][j]);
}
}
size = tam[1];
jump = 1;
jump2 = 0;
for(i=0;i<tam[0];i++)
{
Qsort (matrix,jump2, jump*size-1);
jump++;
jump2 = jump2 + size;
}
print (matrix,tam);
return 0;
}
Thanks,
Favolas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在一遍又一遍地对第一行进行排序。您只想将单个列传递给 Qsort,因此应声明它:
在主函数中,只需对每个连续行调用 Qsort:
It looks like you are sorting the first row over and over. You only want to pass a single column to
Qsort
so it should be declared:In your main function, simply call Qsort with each successive row:
根据此矩阵定义,您的 2x2 数据最终会出现在
matrix[0][0]
、matrix[0][1]
、matrix[1][0 ]
和矩阵[1][1]
。没有什么令人惊讶的,但是当被视为单元格的线性列表时,填充的单元格是:(当然,其中
base_address = &matrix[0][0]
。)您没有办法代码来检查
Qsort()
代码中的后两个单元格。这里有几个结论可以得出。
Qsort()
的入口处打印了数组,您会发现您没有认为应该处理的值。这可以确保您知道如何访问数据准确。)考虑一下矩阵[4][7]的图:
如果要将 6 个字母单元格作为子矩阵传递给函数,则需要传递包含 A 的单元格的地址,你需要知道有 3 列和 2 行,并且你还需要知道整个矩阵的宽度是 7。没有 7,你将无法找到单元格 D、E、F。这是你的缩小版当然是 1000x1000 阵列。
您的子矩阵位于整个矩阵的左上角,但适用相同的规则:您需要起始地址、子矩阵的宽度和高度以及完整矩阵的整体宽度才能访问正确的元素。
(顺便说一句,当我第一次开始解释时,上面的矩阵是 4x6,而不是 4x7。然后解释需要传递的 6 与 2x3 的乘积(子矩阵的大小)无关,所以很棘手,所以我更改了“测试用例”以避免出现问题 - 完全按照“选择良好的测试数据”下的建议。)
如何修复您的代码?
方法有很多,取决于你想做什么。困难的方法是传递重要的矩阵宽度参数。更简单的方法可能是分配一维单元格数组,然后手动计算下标。但是,我不确定你想要什么,给定输入值 14, 13, 12, 11 (相反顺序),输出矩阵应该是......什么?
这意味着您需要交换矩阵的整行。
Given this matrix definition, your 2x2 data ends up in
matrix[0][0]
,matrix[0][1]
,matrix[1][0]
andmatrix[1][1]
. Nothing startling there, but when regarded as a linear list of cells, the populated cells are:(where
base_address = &matrix[0][0]
, of course.)There is no way for your code to examine the latter two cells in the
Qsort()
code.There are several conclusions to draw here.
Qsort()
, you'd see that you don't have the values you thought you should have to work on. This makes sure you know how to access the data accurately.)Consider this diagram of a
matrix[4][7]
:If you want to pass the 6 lettered cells to a function as a sub-matrix, you need to pass the address of the cell containing A, you need to know that there are 3 columns and 2 rows, and you also need to know that the width of the whole matrix is 7. Without that 7, you aren't going to find the cells D, E, F. This is a miniature version of your 1000x1000 array, of course.
Your sub-matrix is the top, left-hand corner of the overall matrix, but the same rules apply: you need start address, width and height of sub-matrix, and overall width of the complete matrix to access the correct elements.
(Incidentally, when I first started this explanation, the matrix above was 4x6, not 4x7. Then explaining that the 6 that needs to be passed was not related to the product of 2x3 - the size of the sub-matrix - got tricky, so I changed my 'test case' to avoid the problem - exactly as suggested under 'choose good test data'.)
How to fix your code?
There are many ways, depending on what you want to do. The hard way passes the vital matrix-width parameter around. The easier ways probably allocate a 1-D array of cells and then calculate the subscripts manually. However, I'm not sure what you want given the input values 14, 13, 12, 11 (reverse order), the output matrix is supposed to be ... what?
This means you need to swap whole rows of your matrix.