在C中对2d矩阵cols和行进行排序

时间:2013-09-18 14:17:32

标签: c arrays sorting

给定二维整数。该数组由5行10列组成。 系统中的每个值都是0到20之间的随机数。 必须编写一个程序来执行数组值的排序,如下所示: 首先,在每列中排列值,使它们按升序排列(从上到下),然后 - 这样就可以通过比较同一行中不同列中的值对来对“右上”列进行排序(“比较词典编纂“):比较第一行中两列中的两个值,如果它们与第二行中的值相比相同,依此类推,并相应地改变列的顺序(参见第三次打印中的示例)数组,下面)。 在排序之前和紧急情况的两个阶段之后显示阵列。 例如 : example and output of my code 我坚持每个cols的排序。我没有得到我想要的排序。我想请你帮忙。 这是我的代码:

#include "stdio.h"
#include "conio.h"
#include "malloc.h"
#include "stdlib.h"

#define N 5
#define M 10
#define LOW 0
#define HIGH 20

void initRandomArray(int arr[N][M]);
void printArray(int arr[N][M]);
void SortInColumn(int arr[N][M],int m);
int main()
{
    int arr[N][M];
    int m;
    m=M;

    srand((unsigned)time(NULL)); //To clear the stack of Random Number
    initRandomArray(arr);
    printf("Before sorting:\n");
    printArray(arr);
    printf("Sorting elements in each column:\n");
    SortInColumn(arr,M);
    system("pause");
    return 0;
}
void initRandomArray(int arr[N][M])
{

    int i,j;
    for (i=0 ; i<N ; i++)
        for (j=0 ; j<M ; j++)
        {
         arr[i][j]=LOW+rand()%(HIGH-LOW+1);
        }

}
void printArray(int arr[N][M])
{ 
    int i,j;
    for (i=0 ; i<N ; i++)
    {
        for (j=0 ; j<M ; j++)
            printf("%d ", arr[i][j]);
         printf("\n");
    }
}
void SortInColumn(int arr[][M],int m)
{
    int i,j;
    int temp;
    for( i=m-1 ; i>=0 ; i--)
        {   
        for(j=0; j<N-1; j++)
            if (arr[i][j]>arr[i][j+1]) // compare adjacent item
                 {
                  temp=arr[i][j];
                  arr[i][j]=arr[i][j+1];
                  arr[i][j+1]=temp;
                 }
        }
        for (i=0 ; i<N ; i++)
        {
            for (j=0 ; j<M ; j++)
                printf("%d ", arr[i][j]);
             printf("\n");
        }
}

2 个答案:

答案 0 :(得分:0)

这是我的,它运行并给出正确的答案。

基本上,你做错了两件事。

  • 你需要三个循环而不是两个循环。外循环围绕每列循环。第二个循环确保您将每列比较N-1次,因为对于每次运行,您在正确的位置获得一个项目。内环进行相邻的比较。

  • 您需要将arr[i][k]arr[i][k+1]arr[i][k]之间的比较更改为arr[i+1][k]。因为您要在同一列中比较它们,所以保持值k(列)不变并更改行i

    void SortInColumn(int arr[][M],int m)
    {
        int i,j,k;
        int temp;
    
        for( k=0 ; k<m ; ++k)
        {
            for(j=0; j<N-1; j++)
            {
                    for(i=0; i < N-1 - j; i++)
                    {
                            if (arr[i][k]>arr[i+1][k]) // compare adjacent item
                            {
                                    temp=arr[i][k];
                                    arr[i][k]=arr[i+1][k];
                                    arr[i+1][k]=temp;
                            }
                    }
            }
        }
    }
    
BTW,这种算法一般性能非常糟糕。你可能想尝试其他的东西。

答案 1 :(得分:-1)

让我们看一下你的SortInColum函数。我已经更改了格式以更好地了解正在发生的事情,并重命名了一些变量。

void SortInColumn(int arr[][M],int m)
{
  int row,col;
  int temp;
  for( row=m-1 ; row>=0 ; row--) // foreach row
  {
    for(col=0; col<N-1; col++) { // foreach column
      if (arr[row][col]>arr[row][col+1]) { // comparing adjacent entries 
                                           //  in different cols? Why? 
        temp=arr[row][col];
        arr[row][col]=arr[row][col+1];
        arr[row][j+1]=temp;
      }
  }
  printArray(arr);

}

此函数(给定一些更改)将对单个列进行排序,然后可以为每个列调用此列。希望这给你一个很好的起点。

相关问题