Python program that uses the sort()
method to sort the elements of an array in ascending order:
def sort_ascending(arr):
arr.sort()
return arr
# Example usage
arr = [5, 3, 8, 4, 1, 9, 6]
print(sort_ascending(arr))
The function sort_ascending(arr)
takes an array as an argument. It uses the sort()
method to sort the elements of the array in ascending order. By default, the sort()
method sorts the elements of the array in ascending order. The function returns the sorted array.
The example usage creates an array and calls the sort_ascending()
function to sort the elements of the array in ascending order, and then it prints the sorted array.
Alternatively, you could use the sorted()
function to sort the elements of an array in ascending order.
def sort_ascending(arr):
return sorted(arr)
This method uses the sorted()
function to sort the elements of the array in ascending order and returns the sorted array.
You could also use the numpy.sort()
function to sort the elements of an array in ascending order, this method is faster for large arrays.
import numpy as np
def sort_ascending(arr):
return np.sort(arr)
This method uses the numpy.sort()
function to sort the elements of the array in ascending order and returns the sorted array.