Python Program to Sort the 2D Array Across Columns


When a two dimensional array or a 2D array is declared, it is treated like a matrix. So, we know that a matrix consists of rows and columns. The process of sorting the elements that belong to a particular column of a matrix either in ascending order or in descending order is known as sorting a 2D array across columns. Let us consider an algorithm followed by an Input output scenario to understand the exact application of this concept.

Input Output Scenario

Consider a two dimensional array.

arr =  [[ 7, 9, 5, 7 ], [9, 5, 9, 4], [2, 7, 8, 6], [ 8, 6, 6, 5]]

The matrix representation of the above two dimensional array is as follows −

7   9   5   7
9   5   9   4
2   7   8   6
8   6   6   5

Now, let us sort the given matrix across columns in descending order.

  • The first column consists of the elements 7, 9, 2, and 8. The descending order of the elements 7, 9, 2, and 8 is 9, 8, 7, and 2.

  • The second column consists of the elements 9, 5, 7, and 6. The descending order of the elements 9, 5, 7, and 6 is 9, 7, 6 and 5.

  • Similarly, the third and fourth columns are also sorted.

  • The sorted matrix across column in descending order is

9   9   5   7
8   7   9   6
7   6   8   5
2   5   6   4 
  • The array representation of the sorted matrix is

  • [[9, 9, 9, 7 ], [7, 7, 8, 6], [8, 6, 6, 5], [ 2, 5, 5, 4 ]]
    
  • This is the resulting sorted array.

Example

In this example, we are going to discuss how to sort the two dimensional array across columns. The steps that must be followed in order to construct the desired program are as follows

  • Step 1 − Declare a two dimensional array

  • Step 2 − Traverse all the elements column wise in order to sort those elements accordingly.

  • Step 3 − Compare the elements of the same column such that if one element is smaller than the other by following the condition.

  • Step 4 − Swap the elements if the condition is not satisfied.

  • Step 5 − Continue the same process until all the elements within the columns are covered and finally print the array which is in sorted form.

def sort_the_array_column_wise(arr):
   for j in range (size):
      for i in range(size - 1):
         if arr[i][j] < arr[i + 1][j]:
            temp = arr[i][j]
            arr[i][j] = arr[i + 1][j]
            arr[i + 1][j] = temp

   for i in range(size):

      for j in range(size):

         print(arr[i][j], end=" ")

      print()

arr = [[7, 9, 5, 7 ], [9, 5, 9, 4], [2, 7, 8, 6], [ 8, 6, 6, 5 ]]

size = len(arr)
print("The array before performing sorting operation is: ")
for i in range(size):
   for j in range(size):

      print(arr[i][j], end=" ")
   print()

print("The array after performing sorting operation is: ")
sort_the_array_column_wise(arr)

Output

The output of the above program is as follows −

The array before performing sorting operation is: 
7 9 5 7
9 5 9 4
2 7 8 6
8 6 6 5
The array after performing sorting operation is:
9 9 9 7
7 7 8 6
8 6 6 5
2 5 5 4

Conclusion

We can clearly see that the output is actually matching with the expected result in the above example. In the same way, the sorting of a 2D array across rows can be done by changing few statements in the above program. This is how the concept of sorting of 2D arrays across columns work.

Updated on: 08-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements