通过2D矩阵Bubble-Sort C

时间:2018-10-25 19:30:10

标签: c matrix 2d

所以,这就是像蛇一样的2D矩阵的排序。

snake matrix

这是我的代码,但是有一些问题。

排序不如我所愿。几乎在每个矩阵大小中都有无限循环。我不明白为什么。 而我需要的东西-是的,气泡分选。 谢谢。

    // BUBBLE SORTING
    do
    {
        swapped = 0;
        for (j = 0; j < M; j++)
        {   
            if (k == 0)
            {
                for (i = 0; i < N; i++)
                {
                    for (i = 0; i < N - 1; i++)
                    {
                        if (a[i][j] > a[i + 1][j])
                        {
                            temp = a[i][j];
                            a[i][j] = a[i + 1][j];
                            a[i + 1][j] = temp;
                            swapped++;
                        }
                    }
                    if ((i = N - 1) && (a[i][j] > a[i][j + 1]))
                    {
                        temp = a[i][j];
                        a[i][j] = a[i][j + 1];
                        a[i][j + 1] = temp;
                        swapped++;
                    }
                }
                k++;
            }
            else if (k == 1)
            {
                for (i = N - 1; i >= 0; i--)
                {
                    for (i = N - 1; i > 0; i--)
                    {
                        if (a[i][j] > a[i - 1][j])
                        {
                            temp = a[i][j];
                            a[i][j] = a[i - 1][j];
                            a[i - 1][j] = temp;
                            swapped++;
                        }
                    }
                    if ((i = 0) && (a[i][j] > a[i][j + 1]))
                    {
                        temp = a[i][j];
                        a[i][j] = a[i][j + 1];
                        a[i][j + 1] = temp;
                        swapped++;
                    }
                }       
                k--;
            }
        }
    }
    while (swapped != 0);

1 个答案:

答案 0 :(得分:0)

方法:将元素从2D数组复制到1D数组,然后在对1D数组执行冒泡排序后将其复制回2D数组。

#include <stdio.h>

int main()
{

    //Here I have taken a matrix with 3 rows and 4 columns.

    int matrix[3][4] = {{2,5,-9,4},
                      {7,10,8,1},
                      {-7,100,5,20}};

  //step-1 copy the 2D array into a 1D array

  int mat[3*4],i,j,k=0;

  for(i=0;i<3;i++)
    {
        for(j=0;j<4;j++)
        {
            mat[k] = matrix[i][j];
            k++;
        }

    }       

  //step-2 perform the bubble sort 

  for(i=0;i<3*4-1;i++)   //3*4<---no of elements 
  {
      for(j=0;j<3*4-1-i;j++)
      {
          if(mat[j] > mat[j+1])
          {
              int temp = mat[j];
              mat[j]=mat[j+1];
              mat[j+1] = temp;        
          }

      }

  }

  //step-3 place it back to the 2D array

  int toggle = 1;

  k=0;

  for(j=0;j<4;j++)
    {
        if(toggle==1)
          {    
            for(i=0;i<3;i++)
            {
              matrix[i][j] = mat[k];
              k++;
            }

            toggle = 0;
          }
        else
          {
             for(i=3-1;i>=0;i--)
            {
                matrix[i][j] = mat[k]; 
                k++;   
            }

            toggle = 1;

          }


    }

  //step-4 print the 2D matrix

  for(i=0;i<3;i++)
    {
        for(j=0;j<4;j++)
        {
            printf("%d ",matrix[i][j]);

        }
        printf("\n");
    }



    return 0;
}

输出:

         -9   5   5   100
         -7   4   7    20
          1   2   8    10