Instagram
youtube
Facebook
Twitter

Sort a NumPy Array in Ascending Order

A Sort a NumPy Array in Ascending Order?
Code Explanation:
Library Import:
numpy is imported as np to use NumPy functions.
Array Creation: A 1D NumPy array arr is created with unsorted elements.
Sorting Function: np.sort(arr) sorts the array in ascending order.
Returns Copy: The function returns a sorted copy of the array without changing the original.
Store Result: The sorted array is stored in the variable sorted_arr.
Display Output: print(sorted_arr) prints the sorted array: [1 2 3 4].

 

Program:

import numpy as np

arr = np.array([3, 1, 4, 2])

sorted_arr = np.sort(arr)

print(sorted_arr)