Python Code:
def sort_matrix(M):
result = sorted(M, key = sum)
return result
matrix1 = [
[1, 2, 3],
[2, 4, 5],
[1, 1, 1]
]
matrix2 = [
[1, 2, 3],
[-2, 4, -5],
[1, -1, 1]
]
print("Original Matrix:")
print(matrix1)
print("\nSort the said matrix in ascending order according to the sum of its rows")
print(sort_matrix(matrix1))
print("\nOriginal Matrix:")
print(matrix2)
print("\nSort the said matrix in ascending order according to the sum of its rows")
print(sort_matrix(matrix2))
Sample Output:
Original Matrix: [ [1, 2, 3], [2, 4, 5], [1, 1, 1] ] Sort the said matrix in ascending order according to the sum of its rows[[1, 1, 1], [1, 2, 3], [2, 4, 5]] Original Matrix: [ [1, 2, 3], [-2, 4, -5], [1, -1, 1] ] Sort the said matrix in ascending order according to the sum of its rows[[-2, 4, -5], [1, -1, 1], [1, 2, 3]]
Splitting a string into a list of substrings:
sentence_string = "my name is George"
sentence_string.split()
print(sentence_string)
When it is required to sort matrix based upon sum of rows, a method is defined that uses ‘sum’ method to determine the result.,Python Program to sort rows of a matrix by custom element count,Program to find diagonal sum of a matrix in Python,Python Program to Sort Matrix Rows by summation of consecutive difference of elements
def sort_sum(row):
return sum(row)
my_list = [
[34, 51],
[32, 15, 67],
[12, 41],
[54, 36, 22]
]
print("The list is :")
print(my_list)
my_list.sort(key = sort_sum)
print("The result is :")
print(my_list)
Output
The list is: [ [34, 51], [32, 15, 67], [12, 41], [54, 36, 22] ] The result is: [ [12, 41], [34, 51], [54, 36, 22], [32, 15, 67] ]
1) Traverse all rows one by one and sort rows in ascending order using simple array sort. 2) Convert matrix to its transpose 3) Again sort all rows, but this time in ascending order. 4) Again convert matrix to its transpose ,Given a matrix, sort the rows of matrix in ascending order followed by sorting the columns in descending order. Examples :, We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. , This work is licensed under Creative Common Attribution-ShareAlike 4.0 International and is attributed to GeeksforGeeks.org
Given a matrix, sort the rows of matrix in ascending order followed by sorting the columns in descending order.
Examples :
Input: a[3][3] = {
{
1,
2,
3
},
{
4,
5,
6
},
{
7,
8,
9
}
};
Output: 7 8 9
4 5 6
1 2 3
Input: a[3][3] = {
{
3,
2,
1
},
{
9,
8,
7
},
{
6,
5,
4
}
};
Output: 7 8 9
4 5 6
1 2 3
Original Matrix: 3 2 1 9 8 7 6 5 4 Matrix After Sorting: 7 8 9 4 5 6 1 2 3
Original Matrix: 3 2 1 9 8 7 6 5 4 Matrix After Sorting: 7 8 9 4 5 6 1 2 3
Sorting algorithm. The default is ‘quicksort’. Note that both ‘stable’ and ‘mergesort’ use timsort under the covers and, in general, the actual implementation will vary with datatype. The ‘mergesort’ option is retained for backwards compatibility.,See numpy.sort for notes on the different sorting algorithms.,When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.,Axis along which to sort. Default is -1, which means sort along the last axis.
>>> a = np.array([ [1, 4], [3, 1] ]) >>> a.sort(axis = 1) >>> a array([ [1, 4], [1, 3] ]) >>> a.sort(axis = 0) >>> a array([ [1, 3], [1, 4] ])
>>> a = np.array([('a', 2), ('c', 1)], dtype = [('x', 'S1'), ('y', int)]) >>>
a.sort(order = 'y') >>>
a
array([(b 'c', 1), (b 'a', 2)],
dtype = [('x', 'S1'), ('y', '<i8')])
Create a row vector and sort its elements in ascending order.,B = sort(A) sorts the elements of A in ascending order.,Create a 2-by-2-by-2 array and sort its elements in ascending order along the third dimension.,Create a matrix and sort each of its rows in ascending order.
A = [9 0 - 7 5 3 8 - 10 4 2]; B = sort(A)
B = 1× 9 - 10 - 7 0 2 3 4 5 8 9
A = [3 6 5;7 - 2 4;1 0 - 9]
A = 3× 3 3 6 5 7 - 2 4 1 0 - 9
B = sort(A, 2)
B = 3× 3 3 5 6 - 2 4 7 - 9 0 1
This program accepts matrix. Then sorts the row in an ascending order & the columns in descending order.,This C Program sorts the rows of the matrix in ascending & columns in descending order. ,This is a C Program to sort rows of the matrix in ascending & columns in descending order.,Here is source code of the C program to sort rows of the matrix in ascending & columns in descending order. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to accept a matrics of order MxN and sort all rows of the
* matrix in ascending order and all columns in descending order
*/
#include <stdio.h>
void main()
{
static int array1[10][10], array2[10][10];
int i, j, k, a, m, n;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter co-efficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array1[i][j]);
array2[i][j] = array1[i][j];
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("After arranging rows in ascending order\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
for (k =(j + 1); k < n; ++k)
{
if (array1[i][j] > array1[i][k])
{
a = array1[i][j];
array1[i][j] = array1[i][k];
array1[i][k] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("After arranging the columns in descending order \n");
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (array2[i][j] < array2[k][j])
{
a = array2[i][j];
array2[i][j] = array2[k][j];
array2[k][j] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array2[i][j]);
}
printf("\n");
}
}
$ cc pgm87.c $ a.out Enter the order of the matrix 3 3 Enter co - efficients of the matrix 3 7 9 2 4 8 5 2 6 The given matrix is 3 7 9 2 4 8 5 2 6 After arranging rows in ascending order 3 7 9 2 4 8 2 5 6 After arranging the columns in descending order 5 7 9 3 4 8 2 2 6
For example, a simple selection sort repeatedly finds the minimum value from a list, and makes swaps until the list is sorted. We can code this in just a few lines of Python:,A useful feature of NumPy's sorting algorithms is the ability to sort along specific rows or columns of a multidimensional array using the axis argument. For example:,Note that the first three values in the resulting array are the three smallest in the array, and the remaining array positions contain the remaining values. Within the two partitions, the elements have arbitrary order.,Keep in mind that this treats each row or column as an independent array, and any relationships between the row or column values will be lost!
import numpy as np
def selection_sort(x):
for i in range(len(x)):
swap = i + np.argmin(x[i: ])
(x[i], x[swap]) = (x[swap], x[i])
return x
x = np.array([2, 1, 4, 3, 5]) selection_sort(x)
array([1, 2, 3, 4, 5])
def bogosort(x):
while np.any(x[: -1] > x[1: ]):
np.random.shuffle(x)
return x
x = np.array([2, 1, 4, 3, 5]) bogosort(x)
x = np.array([2, 1, 4, 3, 5]) np.sort(x)