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.
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 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
The array of sorted matrix is represented as
[[9, 9, 9, 7 ], [7, 7, 8, 6], [8, 6, 6, 5], [ 2, 5, 5, 4 ]]
This is the sorted array.
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)
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
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!