c - 使用列排序对矩阵进行排序
问题描述
根据“算法简介”一书,我尝试使用列排序对矩阵进行排序。这是我的方法
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;
}
输出不太可能如何正确排序
解决方案
这是您的代码的工作版本。我修复了丢弃的括号和转置功能:
#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;
}
以下是排序、转置、排序、然后转置的输出:
推荐阅读
- javascript - 我的网站在 chrome 上的欺骗性网站提前警告
- python - 如何在不按 kivymd 按钮的情况下自动从 MDTextfield 获取数据
- r - Legend position based on coordinates in ggplot2
- python - excel文件中的每两列被分成多个csv文件
- python-3.x - 发生异常:IndexError list index out of range, line 5, in module file_path - sys.argv[1] Python & PyPDF2
- gitlab - Packer 在 Gitlab-runner 上执行时失败
- javascript - 世博会背景获取未运行
- imagemagick - Imagemagick - 逆
- python - 按值排序后如何按字母顺序对字典的键进行排序?
- flutter - 如何在 Flutter 中使用自定义应用栏和图像创建 SliverAppBar?