所以...我想做一个2D选择排序algorythm,但我不知道我做错了什么(C),你能帮帮我吗?
我只需要从第一个数组复制偶数数字到第二个数组,然后通过选择排序对它们进行排序,通常是:
for(x = 0; x < maxlength - 1; x++)
for(y = x + 1; y < maxlength; 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("\n");
show(arrB);
printf("\n");
//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] < arrB[countxB][countyB])
{
temp = arrB[countx][county];
arrB[countx][county] = arrB[countxB][countyB];
arrB[countxB][countyB] = temp;
swapped = 1;
}
}
}
}
}
}
答案 0 :(得分:0)
坦率地说,我很难按照你的代码。部分原因是由于不幸的长名选择。我在这里分享Pablo
情绪。通常,选择短名称i
,j
进行索引。
请允许我说,应该以可以重用函数的方式编写程序。读取和调试代码的一小部分更容易。 main
函数应尽可能短。
它应该只调用已经准备好的函数。
借助1D阵列对2D阵列进行排序在概念上比在适当的位置更简单。我也添加了这个解决方案。
您可能需要查看:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 < ROW; i++)
{
for(j = 0; j < COL; 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\n", mess);
for(i=0 ;i<row; i++)
{
for(j=0; j<col; j++)
printf("%d ",arr[i][j]);
printf("\n");
}
printf("\n");
}
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<ROW; i++)
{
for(j=0; j<COL; j++)
{
z = i;
q = j;
min = arr[i][j];
w = j;
for(k=i; k<ROW; k++)
{
for(; w<COL; w++)
{
if(arr[k][w] < min)
{
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<row;i++){
for(int j=0; j<col; 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<row*col; i++)
{
for(int j=0; j<row*col-1; 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:\n", arrA, ROW, COL );
show("arrB:\n", arrB, ROW, COL );
arrcpy(arrA, arrB);
show("Arr B after copy:\n", arrB, ROW, COL );
sort(arrB, ROW, COL) ;
show("ArrB after sorting:\n", 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("--------------------\nArray to be sorted:\n", arr, ROW, COL );
int result[ROW*COL];
convert2Dto1D(result, arr, ROW, COL);
sort1D(result, ROW, COL );
convert1Dto2D(arr, result, ROW, COL);
show("Sorted array:\n", 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
我希望它有所帮助。