
Sorting is a fundamental operation in programming, and Python provides various efficient ways to sort data. One of the most useful functions in the numpy
library for sorting is numpy.argsort()
. But how does it work? Let’s dive deep into how numpy.argsort()
works in Python with the best examples!
Understanding numpy.argsort()
The numpy.argsort()
function returns the indices that would sort an array. Instead of sorting the values directly, it provides a way to reorder the array by returning the sorted index positions.
Here’s the basic syntax:
numpy.argsort(a, axis=-1, kind='quicksort', order=None)
Where:
a
– The input array.axis
– The axis along which to sort (default is -1, meaning the last axis).kind
– The sorting algorithm:'quicksort'
(default),'mergesort'
,'heapsort'
, or'stable'
.order
– Used when sorting structured arrays.
Basic Example of numpy.argsort()
Let’s start with a simple example demonstrating numpy.argsort()
:
import numpy as np
arr = np.array([30, 10, 50, 20])
sorted_indices = np.argsort(arr)
print("Original array:", arr)
print("Sorted indices:", sorted_indices)
print("Sorted array:", arr[sorted_indices])
### Output
Original array: [30 10 50 20]
Sorted indices: [1 3 0 2]
Sorted array: [10 20 30 50]
Here, the function returns the position of elements in ascending order, which helps us retrieve the sorted values using index-based selection.
Sorting a 2D Array with numpy.argsort()
The function also works with multidimensional arrays. Let’s see how it behaves with 2D arrays.
arr_2d = np.array([[30, 20, 50], [10, 60, 40]])
sorted_indices_2d = np.argsort(arr_2d, axis=1)
print("Original 2D array:")
print(arr_2d)
print("Sorted indices along axis 1:")
print(sorted_indices_2d)
print("Sorted array along axis 1:")
sorted_2d = np.take_along_axis(arr_2d, sorted_indices_2d, axis=1)
print(sorted_2d)
### Example Output
Original 2D array:
[[30 20 50]
[10 60 40]]
Sorted indices along axis 1:
[[1 0 2]
[0 2 1]]
Sorted array along axis 1:
[[20 30 50]
[10 40 60]]
We used np.take_along_axis()
to apply the sorting order returned by argsort()
.
Sorting in Descending Order
By default, numpy.argsort()
sorts in ascending order. If you want descending order sorting, just reverse the order:
arr = np.array([30, 10, 50, 20])
sorted_desc_indices = np.argsort(arr)[::-1]
print("Sorted indices for descending order:", sorted_desc_indices)
print("Sorted array:", arr[sorted_desc_indices])
### Output
Sorted indices for descending order: [2 0 3 1]
Sorted array: [50 30 20 10]
Performance Comparison of Sorting Methods
Different sorting algorithms can be used with numpy.argsort()
. Here’s a comparison:
Sorting Method | Best Case Complexity | Worst Case Complexity | Stable? |
---|---|---|---|
'quicksort' |
O(n log n) | O(n²) | No |
'mergesort' |
O(n log n) | O(n log n) | Yes |
'heapsort' |
O(n log n) | O(n log n) | No |
'stable' (alias for 'mergesort' ) |
O(n log n) | O(n log n) | Yes |
Key Takeaways
Here’s why numpy.argsort()
is useful:
- It provides sorting indices instead of values.
- It helps reorder arrays efficiently.
- It supports different sorting algorithms.
- It can be leveraged for complex sorting tasks (e.g., multi-level sorting).
Now that you know how numpy.argsort()
works in Python, you can start using it in your projects to efficiently sort arrays based on indices!