首页 > 解决方案 > 使用列排序对矩阵进行排序

问题描述

根据“算法简介”一书,我尝试使用列排序对矩阵进行排序。这是我的方法

1)对矩阵的每一行进行排序 -

#include<stdio.h>
#include<stdlib.h>

void sort(int arr[3][3], int k)// here k defines  the column number ,k remains constant through out the function
                                  //because we have to sort the matrix column wise respectively
{
    int i;
    int c[10]={0};
    int b[3][3];
    for(i=0;i<3;i++)
    { c[arr[i][k]]++;
    }
    for(i=1;i<3;i++)
    {
        c[i]+=c[i-1];
    }
    for(i=2;i>=0;i--)
    {
        b[c[arr[i][k]]-1][k]=arr[i][k];
        c[arr[i][k]]--;
    }
    for(i=0;i<3;i++)
    {
        arr[i][k]=b[i][k];
    }
}

我已将 k 作为参数传递,它是列号(因为它在每列的排序过程中保持不变,并且只有行号发生变化,所以我只迭代了行号)

传递所需列号的函数,然后调用 count 函数

  void column(int arr[3][3]) // to call the function column wise by passing k as a parameter to count function
{
    int k;
    for(k=0;k<3;k++)
        sort(arr,k);
}

2) 转置一个矩阵

{
    int i,j,temp;
    for(i=0;i<3;i++)
    {   for(j=0;j<3;j++)
             temp=arr[i][j];
             arr[i][j]=arr[j][i];
             arr[j][i]=temp;
    }
}

打印功能

   void print(int arr[3][3]) // to print the output
{
     int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
                printf("%d ",arr[i][j]);
            }
             printf("\n");
    }
}

主要功能

{
    int arr[3][3];
    int i,j;
    for (i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
           scanf("%d ",&arr[i][j]);
    }
    // prints the array just inputed
    print(arr);
    // column calls the function sort according to the column
    column(arr);
    transpose(arr);  // matrix is transposed
    printf("\n");
    print(arr);      // matrix is printed
    column(arr);     // matrix is again passed with respect to the columns and sorted
    transpose(arr);   // matrix is transposed to get the original matrix
    printf("\n");
    print(arr);     //final result is printed
    return 0;
}

输出不太可能如何正确排序

标签: csortingmatrix

解决方案


这是您的代码的工作版本。我修复了丢弃的括号和转置功能:

#include <stdio.h>
#include<stdlib.h>

void sort(int arr[3][3], int k)// here k defines  the column number ,k remains constant through out the function
                                  //because we have to sort the matrix column wise respectively
{
    int i,j;
    int tmp = 0;
    for(i=0;i<2;i++)
    { 
        for (j = 0; j < 3-i-1; j++) 
        {
           if (arr[j][k] > arr[j+1][k]) 
           {
              tmp = arr[j][k];
              arr[j][k] = arr[j+1][k];
              arr[j+1][k] = tmp;
           }
        }
    }
    printf("\n");
}

void column(int arr[3][3]) // to call the function column wise by passing k as a parameter to count function
{
    int k;
    for(k=0;k<3;k++)
    {
        sort(arr,k);
    }
}

void transpose(int arr[3][3])
{
    int i,j,temp;
    for(i=0;i<3;i++)
    {   
        for(j=0;j<3;j++)
        {
            if(i != j && i<j)
            {
                 temp=arr[i][j];
                 arr[i][j]=arr[j][i];
                 arr[j][i]=temp;
            }
        }
    }
}

void print(int arr[3][3]) // to print the output
{
     int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
                printf("%d ",arr[i][j]);
            }
             printf("\n");
    }
}

int main()
{
    int arr[3][3] = {0};
    int i,j;
    for (i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
           scanf("%d ",&arr[i][j]);
    }
    printf("\n");
    // prints the array just inputed
    print(arr);
    printf("\n");
    // column calls the function sort according to the column
    column(arr);
    print(arr);
    transpose(arr);  // matrix is transposed
    printf("\n");
    print(arr);      // matrix is printed
    column(arr);     // matrix is again passed with respect to the columns and sorted
    printf("\n");
    print(arr);      // matrix is printed
    transpose(arr);   // matrix is transposed to get the original matrix
    printf("\n");
    print(arr);     //final result is printed
    return 0;
}

以下是排序、转置、排序、然后转置的输出:

排序输出


推荐阅读