0
0
NumPydata~5 mins

np.sort() for sorting arrays in NumPy

Choose your learning style9 modes available
Introduction

We use np.sort() to arrange numbers or values in order. This helps us see data clearly and find things like smallest or biggest values easily.

When you want to find the smallest or largest numbers in a list of data.
When you need to organize data before analyzing it, like sorting test scores.
When you want to prepare data for graphs that need ordered values.
When you want to compare data points in order, such as ranking players by scores.
When you want to clean up data by sorting it to spot duplicates or errors.
Syntax
NumPy
import numpy as np

sorted_array = np.sort(array, axis=-1, kind='quicksort', order=None)

array is the input array you want to sort.

axis decides which direction to sort: -1 means sort the last axis (e.g., each row if 2D, or the whole array if 1D).

Examples
This sorts a simple list of numbers from smallest to largest.
NumPy
import numpy as np

# Example 1: Sort a 1D array
array_1d = np.array([3, 1, 4, 1, 5])
sorted_1d = np.sort(array_1d)
print(sorted_1d)
This sorts each row separately, so each row's numbers go from smallest to largest.
NumPy
import numpy as np

# Example 2: Sort a 2D array along rows (axis=1)
array_2d = np.array([[3, 2, 1], [6, 5, 4]])
sorted_2d = np.sort(array_2d, axis=1)
print(sorted_2d)
Sorting an empty array returns an empty array without error.
NumPy
import numpy as np

# Example 3: Sort an empty array
empty_array = np.array([])
sorted_empty = np.sort(empty_array)
print(sorted_empty)
Sorting an array with one element returns the same array.
NumPy
import numpy as np

# Example 4: Sort a 1D array with one element
single_element_array = np.array([42])
sorted_single = np.sort(single_element_array)
print(sorted_single)
Sample Program

This program shows how to sort each row of a 2D array. Each row represents a student's scores, and sorting helps us see their scores from lowest to highest.

NumPy
import numpy as np

# Create a 2D array
scores = np.array([[88, 92, 79], [95, 85, 91], [70, 78, 88]])

print("Original scores:")
print(scores)

# Sort each student's scores (each row) from lowest to highest
sorted_scores = np.sort(scores, axis=1)

print("\nSorted scores by student:")
print(sorted_scores)
OutputSuccess
Important Notes

Time complexity: np.sort() usually runs in O(n log n) time, where n is the number of elements to sort.

Space complexity: It creates a new sorted array, so it uses extra memory equal to the size of the input array.

A common mistake is forgetting to specify axis when sorting multi-dimensional arrays, which can lead to unexpected sorting directions.

Use np.sort() when you want a sorted copy of the array without changing the original. Use array.sort() if you want to sort the array in place.

Summary

np.sort() helps arrange data in order, making it easier to analyze.

You can sort 1D or multi-dimensional arrays by choosing the right axis.

It returns a new sorted array, leaving the original data unchanged.