Variations in different Sorting techniques in Python
0 1204
Introduction
Sorting is a fundamental operation in data analysis, allowing us to arrange data in a specified order. NumPy, a powerful library for numerical computations in Python, offers several functions to sort arrays efficiently. In this article, we'll delve into three such functions:sort(), argsort(), and lexsort(), understanding their differences and appropriate use cases.
1. sort() – In-Place Sorting
The sort() method sorts the elements of an array in ascending order, modifying the original array.
import numpy as np
a = np.array([9, 3, 1, 7, 4, 3, 6])
print("Original array:", a)
a.sort()
print("Sorted array:", a)
Output:
Original array: [9 3 1 7 4 3 6]
Sorted array: [1 3 3 4 6 7 9]
Key Points:
- Sorts the array in-place.
- Does not return a new array; returns
None. - Modifies the original array.
- Faster than
sorted()for NumPy arrays.
2. argsort() – Indirect Sorting
The argsort() function returns the indices that would sort an array.
import numpy as np
a = np.array([9, 3, 1, 7, 4, 3, 6])
indices = np.argsort(a)
print("Sorted indices:", indices)
sorted_array = a[indices]
print("Sorted array:", sorted_array)
Output:
Sorted indices: [2 1 5 4 6 3 0]
Sorted array: [1 3 3 4 6 7 9]
Key Points:
- Returns the indices that would sort the array.
- Useful for sorting arrays based on the order of elements in another array.
- Does not modify the original array.
3. lexsort() – Lexicographical Sorting
The lexsort() function performs an indirect stable sort using a sequence of keys.
import numpy as np
a = np.array([9, 3, 1, 3, 4, 3, 6])
b = np.array([4, 6, 9, 2, 1, 8, 7])
indices = np.lexsort((b, a))
sorted_array = np.column_stack((a[indices], b[indices]))
print("Sorted array by a then b:", sorted_array)
Output:
Sorted array by a then b:
[[3 2]
[3 6]
[3 8]
[1 9]
[4 1]
[6 7]
[9 4]]
Key Points:
- Performs lexicographical sorting using multiple keys.
- The last key in the tuple is the primary sort key.
- Useful for sorting structured data based on multiple criteria.
- Returns the indices that sort the keys along the specified axis.
Choosing the Right Sorting Function
Understanding when to use each sorting function is crucial for efficient data manipulation:- Use
sort()when you need to sort the array in-place and do not require the original order. - Use
argsort()when you need the indices that would sort the array, especially when sorting based on another array. - Use
lexsort()when you need to perform lexicographical sorting using multiple keys.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments