как исправить сортировку вставками?

Подскажите пожалуйста, как исправить этот код, а то сортировка через раз работает. я проверяю на этих матрицах. это язык си

1  2  3  4  5  6  7  8
2  3  4  5  6  7  8  9
3  4  5  6  7  8  9  10
4  5  6  7  8  9  10 11
5  6  7  8  9  10 11 12
6  7  8  9  10 11 12 13
7  8  9  10 11 12 13 14

9  4  7  2  5  1  8  3
6  3  5  1  2  7  9  4
8  1  4  3  9  6  2  5
2  9  6  8  7  3  4  1
3  7  8  4  1  2  5  6
5  2  1  6  8  9  7  4
4  8  3  5  6  4  1  2

вот мой код

#include <stdio.h>
 
void insertionSort(int arr[], int n) {
    int i, key, j;
    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;
 
        // Move elements of arr[0..i-1] that are greater than key
        // to one position ahead of their current position
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}
 
void sortColumns(int A[][10], int m, int n) {
    for (int j = 0; j < n; j++) {
        int column[m];
        for (int i = 0; i < m; i++) {
            column[i] = A[i][j];
        }
 
        insertionSort(column, m);
 
        for (int i = 0; i < m; i++) {
            A[i][j] = column[i];
        }
    }
}
 
void printMatrix(int A[][10], int m, int n) {
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", A[i][j]);
        }
        printf("\n");
    }
}
 
int main() {
    int m, n;
    // Input matrix dimensions
    printf("Enter the number of rows (m): ");
    scanf("%d", &m);
    printf("Enter the number of columns (n): ");
    scanf("%d", &n);
 
    int A[m][10]; // The array can have at most 10 columns
 
    // Input matrix elements
    printf("Enter the elements of the matrix:\n");
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &A[i][j]);
        }
    }
 
    // Sort columns
    sortColumns(A, m, n);
 
    // Print the sorted matrix
    printf("\nSorted Matrix:\n");
    printMatrix(A, m, n);
 
    return 0;
}

скорее всего ошибка где-то в самой функции, но я не пойму в чём дело


Ответы (0 шт):