是否有可能按升序排序,在C二维数组?如果是这样,怎么样? [英] Is it possible to sort in ascending order, a 2D array in C? If so, how?
问题描述
我的任务之一是进行排序二维数组按升序进行,我无法弄清楚如何做到这一点对我的生活。
我到目前为止有:
INT Sort2DArray(INT A [] [COL],无符号整型ROWSIZE,无符号整型colsize)
{
INT I,J,K,温度;
对于(i = 0; I< ROWSIZE-1;我++){
对于(K = 0; K< colsize; k ++){
为(J = 0; J< ROWSIZE-1; J ++){
做{
温度= A [K] [J]。
A [k]的[J] = A [k]的[J + 1];
A [k]的[J + 1] =温度;
}而(A [k]的[J]>将[K] [J + 1]);
}
}
}
}
这将需要一个数组它并返回:
3 2 1 1 2 3
5 8 7 ---->>> 5 7 8
4 9 3 3 4 9
不过,我需要它返回:
1 2 3
4 5 6
7 8 9
那么,有没有什么办法你们能帮助我吗?谢谢!
编辑:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;#定义COL 20
#定义ROW 20无效PopulateArray2DUnique(INT [] [COL],无符号整型,无符号整型,INT,INT);无效DisplayArray2D(INT [] [COL],无符号整型,无符号整型);INT FindLargest(INT [] [COL],无符号整型,无符号整型);INT FindColSum(INT [] [COL],无符号整型,无符号整型,无符号整型);INT Sort2DArray(INT [] [COL],无符号整型,无符号整型);诠释的main()
{
INT A [ROW] [COL];
INT分= 1,最大值= 99;
unsigned int类型ROWSIZE,colsize,col_to_sum; 的printf(请输入您想要的行和列的大小:\\ n);
scanf函数(%U%U,&安培; colsize,&安培; ROWSIZE); PopulateArray2DUnique(A,ROWSIZE,colsize,最小值,最大值);
DisplayArray2D(A,ROWSIZE,colsize);
FindLargest(A,ROWSIZE,colsize); 的printf(你会喜欢哪栏里找到的总和\\ n吗?);
scanf函数(%d个,&安培; col_to_sum); FindColSum(A,ROWSIZE,colsize,col_to_sum);
Sort2DArray(A,ROWSIZE,colsize);
DisplayArray2D(A,ROWSIZE,colsize); 返回0;
}
我不知道如果我这里有最好的方法,但是我还能做什么,从阵列中的每个值存储到一个大的一维数组,排序这一点,然后将它们分配给二维数组。
INT Sort2DArray(INT A [] [COL],无符号整型ROWSIZE,无符号整型colsize)
{
INT ARRAYSIZE = ROWSIZE * colsize;
INT sortingArray [ARRAYSIZE]
INT I = 0,行,列,温度,prevPos; //填充的sortingArray与2D阵列中的所有值
对于(COL = 0;&山坳下,colsize ++ COL){
为(行= 0;&行LT; ROWSIZE ++行){
sortingArray [I] = A [行] [COL];
++我;
}
} //排序的一维数组(插入排序)
对于(i = 1; I< ARRAYSIZE ++ I)
{
TEMP = sortingArray [I]
prevPos = I - 1;
而(J> = 0&放大器;&放大器; sortingArray [prevPos]≥温度)
{
sortingArray [prevPos + 1] = sortingArray [prevPos]。
prevPos = prevPos - 1;
}
sortingArray [prevPos + 1] =温度;
} //写数据返回到二维数组
I = 0;
为(行= 0;&行LT; ROWSIZE ++行){
对于(COL = 0;&山坳下,colsize ++ COL){
A [行] [山口] = sortingArray [I]
++我;
}
}
}
我希望我没有得到所有这些方面太混乱了,但你的想法。如果你发现任何不正确,让我知道。
Part of my assignment is to sort a 2D array into ascending order, and I cannot figure out how to do it for the life of me.
What I have so far:
int Sort2DArray(int A[][COL], unsigned int rowsize, unsigned int colsize)
{
int i, j, k, temp;
for (i=0; i<rowsize-1; i++){
for (k=0; k<colsize; k++){
for (j=0; j<rowsize-1; j++){
do {
temp = A[k][j];
A[k][j] = A[k][j+1];
A[k][j+1] = temp;
} while (A[k][j]>A[k][j+1]);
}
}
}
}
This will take an array this and return:
3 2 1 1 2 3
5 8 7 ---->>> 5 7 8
4 9 3 3 4 9
However, I need it to return:
1 2 3
4 5 6
7 8 9
So, is there any way you guys can help me? Thanks!
EDIT:
#include <stdio.h>
#include <stdlib.h>
#define COL 20
#define ROW 20
void PopulateArray2DUnique (int [][COL], unsigned int, unsigned int, int, int);
void DisplayArray2D(int [][COL], unsigned int, unsigned int);
int FindLargest(int [][COL], unsigned int, unsigned int);
int FindColSum(int [][COL], unsigned int, unsigned int, unsigned int);
int Sort2DArray(int [][COL], unsigned int, unsigned int);
int main()
{
int A[ROW][COL];
int min=1, max=99;
unsigned int rowsize, colsize, col_to_sum;
printf ("Input your desired row and column size: \n");
scanf ("%u%u", &colsize, &rowsize);
PopulateArray2DUnique(A, rowsize, colsize, min, max);
DisplayArray2D(A, rowsize, colsize);
FindLargest(A, rowsize, colsize);
printf ("Which column would you like to find sum of?\n");
scanf ("%d", &col_to_sum);
FindColSum(A, rowsize, colsize, col_to_sum);
Sort2DArray(A, rowsize, colsize);
DisplayArray2D(A, rowsize, colsize);
return 0;
}
I'm not sure if I have the best method here, however what I would do, is store each value from the array into one large 1D array, sort that and then assign them to the 2D array.
int Sort2DArray(int A[][COL], unsigned int rowsize, unsigned int colsize)
{
int arraySize = rowsize * colsize;
int sortingArray[arraySize];
int i = 0, row, col, temp, prevPos;
//Fills the sortingArray with all the values in the 2D array
for (col = 0; col < colsize; ++col) {
for (row = 0; row < rowsize; ++row) {
sortingArray[i] = A[row][col];
++i;
}
}
//Sorts the 1D array (Insertion Sort)
for (i = 1; i < arraySize; ++i)
{
temp = sortingArray[i];
prevPos = i - 1;
while (j >= 0 && sortingArray[prevPos] > temp)
{
sortingArray[prevPos+1] = sortingArray[prevPos];
prevPos = prevPos - 1;
}
sortingArray[prevPos + 1] = temp;
}
//Writes data back into 2D array
i = 0;
for (row = 0; row < rowsize; ++row) {
for (col = 0; col < colsize; ++col) {
A[row][col] = sortingArray[i];
++i;
}
}
}
I hope I didn't get too confusing with all those dimensions, but you get the idea. If you spot anything incorrect, let me know.
这篇关于是否有可能按升序排序,在C二维数组?如果是这样,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!