- Related Questions & Answers
- A += B Assignment Riddle in Python
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in Python
- Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in C++
- Update 'a' record with 'b' and 'b' with 'a' in a MySQL column (swap) with only 'a' and 'b' values?
- How to generate a sorted list in Python?
- Check if a + b = c is valid after removing all zeroes from a, b and c in C++
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
- Find four elements a, b, c and d in an array such that a+b = c+d in C++
- Kth Smallest Element in a Sorted Matrix in Python
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B in C++
- Check If a Number Is Majority Element in a Sorted Array in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Ordering of data elements in a specific order is a frequently needed operation. To sort elements in an array, python uses the functions named sorted() and array.sort().

This function returns a sorted array without modifying the original array.

a = [9,5,3,1,12,6] b = sorted([9,5,3,1,12,6]) print "Sorted Array :\n", print (b) print "Original Array :\n", print (a)

Running the above code gives us the following result −

Sorted Array : [1, 3, 5, 6, 9, 12] Original Array : [9, 5, 3, 1, 12, 6]

The sort function returns a sorted array by doing in-place modification to the array supplied. Hence the original array gets modified as shown in the example below.

a = [9,5,3,1,12,6] print "Original Array :\n", print (a) print "Sorted Array :\n", a.sort() print (a)

Running the above code gives us the following result −

Original Array : [9, 5, 3, 1, 12, 6] Sorted Array : [1, 3, 5, 6, 9, 12]

So sorted() function is slower than sort() as it creates a copy of the original array and then modifies it.

More complex sorting requirements are done using Numpy. Numpy is a python library which is extensively used in scientific data processing as it provides lots of advanced features. We will see both pure python sorting methods and Numpy sorting methods in the below examples.

This function in numpy returns the indices of the sorted array instead of the array elements. In the below example we take the array, print its elements along with the index for each element. Then we apply the argsort function which gives us the indices of sorted array as a result and the result is also an array.

import numpy as np x = np.array([9,5,3,1,12,6]) print(x) #Print the positions of elements for i in range(len(x)): print "[",i,"]",x[i], print "\n" # Print the indices of sorted elements s = np.argsort(x) print(s)

Running the above code gives us the following result −

[ 9 5 3 1 12 6] [ 0 ] 9 [ 1 ] 5 [ 2 ] 3 [ 3 ] 1 [ 4 ] 12 [ 5 ] 6 [3 2 1 5 0 4]

This function is used for sorting using multiple sort keys involving more than one array. For example, we first sort data in Column A and then sort the values in column B. In the below example we take two arrays representing column A and column B. On applying lexsort() function for sorting first by column A and then by column B we get the result of sorting as an array containing the indices of the elements in column A.

import numpy as np colA = [2,5,1,8,1] # First column colB = [9,0,3,2,0] # Second column # Sort by ColA and then by colB sorted_index = np.lexsort((colB,colA)) print(sorted_index) #print the result showing the #column values as pairs print [(colA[i],colB[i]) for i in sorted_index]

Running the above code gives us the following result −

[4 2 0 1 3] [(1, 0), (1, 3), (2, 9), (5, 0), (8, 2)]

As you can see the lowest two values in colA are 1 and 1 at the index position 2 and 4. But the result shows 4 and 2 as the ascending order as the respective values in column B which are 0 and 3 are also sorted as first 0 then 3, making the result as 4 and 2.

Advertisements