简体   繁体   中英

Sorting 2D array using selection sort

I've used the following code -

x[][] is the array to be sorted

    void sort() {
    int max1, max2, s;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++) {
            max1 = i;
            max2 = j;
            for (int k = i; k < row; k++) {
                if (k == i) {   // need to improve this part
                    for (int l = j; l < column; l++) {
                        if (x[k][l] > x[max1][max2]) {
                            max1 = k;
                            max2 = l;
                        }
                    }
                } else {
                    for (int l = 0; l < column; l++) {
                        if (x[k][l] > x[max1][max2]) {
                            max1 = k;
                            max2 = l;
                        }
                    }
                }
            }
            s = x[max1][max2];
            x[max1][max2] = x[i][j];
            x[i][j] = s;
        }
    }
}

I want to remove the if else statement or is this another way to do it using selection sort?

(I know there are simpler ways to do this but I'm trying to do it with selection sort)

I really do not like how you name your variables. I suggest using r, c as prefixes for row and column, v for value, and then using Outer and Inner as suffixes for the two loops over the array, and Max for the maximum.

Using vMax increases the memory locality of your algorithm - by avoiding lookups to distant parts of memory, it should execute slightly faster when dealing with large arrays. Not that select-sort is ever going to be competitive when compared to, say, quickSort: asymptotic complexity will remain horrible.

This makes everything easier to read:

for (int rOuter = 0; rOuter < rows; rOuter++) {
    for (int cOuter = 0; cOuter < cols; cOuter++) {
        // find max value, and swap with rOuter, cOuter            
        int rMax = rOuter;
        int cMax = cOuter;
        int vMax = x[rOuter][cOuter];
        // finish the row current row
        for (int cInner = cOuter+1; cInner < cols; cInner++) {
            if (x[rOuter][cInner] > vMax) {
                rMax = rOuter; 
                cMax = cInner; 
                vMax = x[rOuter][cInner];
            }
        }
        // evaluate remaining rows
        for (int rInner = rOuter+1; rInner < rows; rInner++) {
            for (int cInner = 0; cInner < cols; cInner++) {
                if (x[rInner][cInner] > vMax) {
                   rMax = rInner; 
                   cMax = cInner; 
                   vMax = x[rInner][cInner];
                }
            }
        }            
        // swap. No aux needed, as vMax contains value of max
        x[rMax][cMax] = x[rOuter][cOuter];
        x[rOuter][cOuter] = vMax;
    }
 }

Note that I have removed the innermost conditional: it should be executed once, and only once, to finish the current row before evaluating all remaining rows.

Also note - I have not tested this code. It looks correct, but treat it with caution.

Like I mentioned I don't have the medium to test this currently, but I believe this should work. If not let me know and I will alter it accordingly.

Here is my solution:

void sort() {
 int max1, max2, count, minValue, rowHold, colHold;
 int[][] tempArr = new int[row][column]; 
 while(count != (row*column)) 
 {
    for (int i = 0; i < row; i++) 
    {
            for (int j = 0; j < column; j++) 
        {
                max1 = i;
                max2 = j;
            if(minValue>x[i][j] && x[i][j] != tempArry[i][j])
            {
                minvalue= x[i][j];
            }



        }

    }

    tempArr[rowHold][colHold] = minVlaue;
    if(colHold<column)
    {
    colHold++;
    }
    else if(colHold=column)
    {
        colHold = 0;
        rowHold++;
    }
    count++;
}   
x = tempArry;

}

This solution preforms a selection sort and many times more efficiently than your current solution. The problem with your current solution is it has way too many nested loops. Nested loops create huge problems in terms of efficiency so you want to minimize the use and depth of them.

Hope this works, comment on this if it does not and I will fix it! Also any questions on what I did and I will comment and explain what I did.

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