作者:淘老婆桃桃_267 | 来源:互联网 | 2023-09-23 13:49
篇首语:本文由编程笔记#小编为大家整理,主要介绍了2D阵列选择排序相关的知识,希望对你有一定的参考价值。
所以...我想做一个2D选择排序algorythm,但我不知道我做错了什么(C),你能帮帮我吗?
我只需要从第一个数组复制偶数数字到第二个数组,然后通过选择排序对它们进行排序,通常是:
for(x = 0; x for(y = x + 1; y {
if(arr[a] > arr[b]
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
事情是我无法将其转换为2D这是代码:
void arrcpy(int arrA[3][5], int arrB[3][5])
{
int countx, county;
int countxB = 0, countyB = 0;
int temp;
int tempx, tempy;
int swapped = 0;
for(countx = 0; countx <3; countx++)
{
for(county = 0; county <5; county++)
if(arrA[countx][county] % 2 == 0 && arrA[countx][county] != 0)
{
arrB[countxB][countyB] = arrA[countx][county];
if((countyB) / 4 == 1)
{
countxB++;
countyB = 0;
}
else
countyB++;
}
}
printf("
");
show(arrB);
printf("
");
//Works up to now
for(countx = 0; countx <3; countx++)
for(county = 0; county <5; county++)
if(arrB[countx][county] % 2 == 0)
{
tempx = countx;
tempy = county;
}
countx = county = countxB = 0;
countyB = 1;
//Works up to now
for(countx = 0; countx <3; countx++)
{
for(county = 0; county <5; county++)
{
swapped = 0;
for(countxB = 0; countxB <3; countxB++)
{
if(swapped)
break;
for(countyB = 0; countyB <5; countyB++)
{
if(swapped)
break;
if(arrB[countx][county] {
temp = arrB[countx][county];
arrB[countx][county] = arrB[countxB][countyB];
arrB[countxB][countyB] = temp;
swapped = 1;
}
}
}
}
}
}
答案
坦率地说,我很难遵循你的代码。部分原因是由于不幸的长名选择。我在这里分享Pablo
情绪。通常,选择短名称i
,j
进行索引。
请允许我说,应该以可以重用函数的方式编写程序。读取和调试代码的一小部分更容易。 main
功能应尽可能短。它应该只调用已经准备好的函数。
在1D阵列的帮助下对2D阵列进行排序在概念上比在适当的位置更简单。我也添加了这个解决方案。
你可能想看看:
#include
#include
#include
#define ROW 3
#define COL 5
void arrcpy(int arrA[ROW][COL], int arrB[ROW][COL])
{
// copy from arrA to arrB ( arrA -> arrB )
// copy only even numbers from the first to the second array but not 0
int i, j;
for(i = 0; i {
for(j = 0; j if(arrA[i][j] % 2 == 0 && arrA[i][j] != 0)
{
arrB[i][j] = arrA[i][j];
}
}
}
void show(char *mess, int arr[ROW][COL], int row, int col )
{
int i;
int j;
printf("%s
", mess);
for(i=0 ;i {
for(j=0; j printf("%d ",arr[i][j]);
printf("
");
}
printf("
");
}
void sort(int arr[ROW][COL], int row, int col)
{
int min,i,j,tmp,y,k,w;
int z=0, q=0;
for(i=0; i {
for(j=0; j {
z = i;
q = j;
min = arr[i][j];
w = j;
for(k=i; k {
for(; w {
if(arr[k][w] {
min = arr[k][w];
z = k;
q = w;
}
}
w = 0;
}
tmp = arr[i][j];
arr[i][j] = min;
arr[z][q] = tmp;
}
}
}
//-------------------------------
void convert2Dto1D(int arr[ROW*COL], int matrix[ROW][COL], int row, int col)
{
// convert 2D array into 1D array
int k=0;
for(int i=0; i for(int j=0; j arr[k++] = matrix[i][j];
}
}
}
void convert1Dto2D(int matrix[ROW][COL], int arr[ROW*COL], int row, int col)
{
// convert 1D array into 2D array
memcpy(matrix, arr, row*col * sizeof(int));
}
void sort1D(int arr[ROW*COL], int row, int col )
{
// sort 1D array
int temp;
for(int i=0; i {
for(int j=0; j {
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//----------------------------------------------------
int main()
{
int arrA[ROW][COL] = {{9,8,2,10,14},{4,5,1,11,0},{6,7,3,12,0}};
int arrB[ROW][COL] = {{19,18,12,110,114},{14,15,11,111,115},{161,17,13,112,113}};
show("arrA:
", arrA, ROW, COL );
show("arrB:
", arrB, ROW, COL );
arrcpy(arrA, arrB);
show("Arr B after copy:
", arrB, ROW, COL );
sort(arrB, ROW, COL) ;
show("ArrB after sorting:
", arrB, ROW, COL );
// Sorting using 1D array
int arr[ROW][COL] = {{9,8,2,10,14},{4,5,1,11,0},{6,7,3,12,0}};
show("--------------------
Array to be sorted:
", arr, ROW, COL );
int result[ROW*COL];
convert2Dto1D(result, arr, ROW, COL);
sort1D(result, ROW, COL );
convert1Dto2D(arr, result, ROW, COL);
show("Sorted array:
", arr, ROW, COL );
return 0;
}
输出:
arrA:
9 8 2 10 14
4 5 1 11 0
6 7 3 12 0
arrB:
19 18 12 110 114
14 15 11 111 115
161 17 13 112 113
Arr B after copy:
19 8 2 10 14
4 15 11 111 115
6 17 13 12 113
ArrB after sorting:
2 4 6 8 10
11 12 13 14 15
17 19 111 113 115
--------------------
Array to be sorted:
9 8 2 10 14
4 5 1 11 0
6 7 3 12 0
Sorted array:
0 0 1 2 3
4 5 6 7 8
9 10 11 12 14
我希望它有所帮助。