简体   繁体   中英

2D Array Selection Sort

So... I wanted to make a 2D selection sort algorythm, but I don't get what I am doing wrong (C), can you help me?

I need to copy only even numbers from the first to the second array, and then sort them through selection sort, which is usually:

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;
    }

Thing is that I can't traspose it into 2D This is the code:

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;
                    }
                }
            }
        }
    }
}

Frankly, it was difficult for me to follow your code. Partially due to unfortunate long name selection. I share Pablo sentiment here. Typically, short names i , j are chosen for indexing.

Allow me to say that one should write the program in such way that functions can be reused. It is easier to read and debug small portions of the code. The main function should be as short as possible. It should just call the already prepared functions.

Sorting 2D array with the help of 1D array is conceptually much simpler then doing it in place. I have added that solution too.

You may want to take a look at:

#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;
}

Output:

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

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM