二维指针数组冒泡排序

void foo()
{

    int k;
    int** p = (int**)malloc(8 * sizeof(int*));

    printf("orgi number:\n");
    for (int i = 0; i < 8; i++)
    {
        p[i] = (int*)malloc(8 * sizeof(int));
        for (int j = 0; j < 8; j++)
        {
            p[i][j] = rand() % 100 + 1;
            printf("%d  ", p[i][j]);
        }
        putchar('\n');
    }
    // sort
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            for (int m = i; m < 8;m++)
            {
                int n = 0;
                if (m == i)
                    n = j + 1;
                for (; n < 8;n++)
                {
                    if (p[i][j] < p[m][n])
                    {
                        p[i][j] = p[i][j] + p[m][n];
                        p[m][n] = p[i][j] - p[m][n];
                        p[i][j] = p[i][j] - p[m][n];
                    }
                }
            }
        }
    }
    printf("sort number:\n");
    for (int i = 0; i < 8; ++i)
    {
        for (int j = 0; j < 8; ++j)
        {
            printf("%d ",p[i][j]);
        }
        printf("\n");
    }



}

定义[8][8]的矩阵再进行从大到小冒泡排序,我按照一开始的写法三层for循环,得到的结果是从大到小,但是只是单行的,而不是想象中的那种从头排到尾,在得到讲解后知道这种写法

你可能感兴趣的:(二维指针数组冒泡排序)