简体   繁体   English

C语言编程。 对2D数组中的行进行排序

[英]C programming. Sorting rows in a 2D array

I'm trying to sort the elements within the individual rows of a 2D array. 我正在尝试对2D数组的各个行中的元素进行排序。 I understand how to sort the elements inside a 1D array, but I am having serious trouble getting it to sort the 2D . 我了解如何对1D数组中的元素进行排序,但是我很难使它对2D数组进行排序。

Code for the 1D array: 1D数组的代码:

for (i = 0; i < size; i++)
{
    for (j = i +1; j < size; ++j)
    {
        if (array2[i] > array2[j])
        {
            swap = array2[i];
            array2[i] = array2[j];
            array2[j] = swap;
        }
    }
}

What I want to do: 2D Array before sorting 我想做的:排序前的2D阵列

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

2D array after sorting: 排序后的2D数组:

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

My code for the 2D so far: 到目前为止,我的2D代码是:

size: the user defined dimensions (in the above case it is 6) size:用户定义的尺寸(在上述情况下为6)

for (i = 0; i < size; i++)
{
    for (j = 0; j < size; j++)
    {
        if(array[i][j] > array[i][j+1])
        {
            swap = array[i][j];
            array[i][j] = array[i][j+1];
            array[i][j+1] = swap;
        }
    }
}

Any help or advice would be much appreciated. 任何帮助或建议,将不胜感激。 Thank you all. 谢谢你们。

If you want to use your single array sorting algorithm (bubble sort) to sort the two dimensional array then you have to add the another for loop: An outer for loop which will take care of each row. 如果要使用单个数组排序算法(bubble sort)对二维数组进行排序,则必须添加另一个for循环:外层for循环,它将处理每一行。 Let's say m is number of row and n is number of column. 假设m是行数, n是列数。

for(k=0; k< m; k++) {
   for (i = 0; i < n; i++) {
     for (j = i +1; j < n; ++j) {
       if (array2[k][i] > array2[k][j])  {
           int swap = array2[k][i];
           array2[k][i] = array2[k][j];
           array2[k][j] = swap;
       }
     }
   } 
}

But this is not an efficient approach to sort the array, it's time complexity will be O(mn^2) 但这不是对数组进行排序的有效方法,它的时间复杂度为O(mn^2)

copy all the elements of the 2d array into an 1d array then apply any sorting algorithm on 1d array & then copy back the sorted 1d array to the 2d array. 将2d数组的所有元素复制到1d数组中,然后对1d数组应用任何排序算法,然后将排序后的1d数组复制回2d数组。 please don't mind if you have a better solution then post it that will be helpfull for me. 如果您有更好的解决方案,请不要介意,然后发布对我有帮助的解决方案。

You can simply use STL to sort 2D array row-wise.. 您可以简单地使用STL对2D数组进行逐行排序。

for (i=0;i<n;i++){
    for ( j=0;j<n;j++){
        cin>>a[i][j];
    }
    sort(a[i],a[i]+n);
  }
int tmp,l;
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
        tmp = a[i][j];
        l = j + 1;
        for (int k = i; k < 2; k++) {
            while (l < 2) {
                if (tmp < a[k][l]) {
                    tmp = a[k][l];
                    a[k][l] = a[i][j];
                    a[i][j] = tmp;
                }
                l++;
            }
            l = 0;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM