2

我试图使用冒泡排序对一个参差不齐的表进行排序。我的算法只对第一行进行排序,但其余行未排序。我认为问题在于“最后一个”变量没有正确初始化。

void bubbleSort (int **table)
{
// Local Declarations
    int temp;
    int current;
    int walker;
    int column;
    int row=0;
    int numCol = 0;
    int last = *table[numCol];

// Statements
    while (table[row] != NULL) {
        for (column = 2; column <= *table[row]; column++) {
            for (current = 2; current < last; current++) {
                bool swapsOccured = false;
                for (walker = last; walker > current; walker--) {
                    if (table[row][walker] > table[row][walker - 1]) {
                        swapsOccured = true;
                        temp                   = table[row][walker];
                        table[row][walker]     = table[row][walker - 1];
                        table[row][walker - 1] = temp;
                        } // if
                    } // for walker
                if (!swapsOccured)
                    return;
                } // for current
            }
            row++;
        } // while
    return;
}
4

0 回答 0