Home > Backend Development > Python Tutorial > Python program to sort 2D array by columns

Python program to sort 2D array by columns

王林
Release: 2023-09-15 22:57:03
forward
774 people have browsed it

Python program to sort 2D array by columns

When a two-dimensional array or a two-dimensional array is declared, it is treated as a matrix. So, we know that a matrix consists of rows and columns. The process of sorting the elements belonging to a specific column of a matrix in ascending or descending order is called sorting a 2D array across columns. Let us consider an algorithm and an input-output scenario to understand the exact application of this concept.

Input and output scenarios

Consider a two-dimensional array.

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

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
Copy after login

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

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

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

    < /里>
  • Similarly, the third and fourth columns are also sorted.

  • The matrix sorted in descending order across columns is

9   9   5   7
8   7   9   6
7   6   8   5
2   5   6   4 
Copy after login
  • The array of sorted matrix is ​​represented as

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

Example

In this example, we will discuss how to sort a two-dimensional array across columns. The steps that must be followed to build the required program are as follows

  • Step 1 - Declare a two-dimensional array

  • Step 2 - Iterate through all the elements column-wise in order to sort the elements accordingly.

  • Step 3 - Compare elements of the same column and follow the condition if one element is less than the other.

  • Step 4 - If the condition is not met, swap the elements.

  • Step 5 - Continue the same process until all elements in the column are covered and finally print the array 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)
Copy after login

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
Copy after login

in conclusion

We can clearly see that the output actually matches the expected result in the example above. Likewise, sorting a two-dimensional array across rows can be accomplished by changing a few statements in the above program. This is how the concept of sorting a 2D array across columns works.

The above is the detailed content of Python program to sort 2D array by columns. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template