Articles

sort numpy array in ascending & descending order | sort function

sort numpy array in ascending & descending order | sort function


In this numpy tutorial, we will discuss about:

  • sort function in numpy,
  • sort numpy array in ascending order,
  • sort numpy array in descending order

Before we learn to sort numpy array in python, lets create a numpy array.

 

numpy stands for numeric python which is used to perform mathematical operations on arrays.

It is a module in which we have to import from the python. 

Syntax to import:

import numpy

We can also use alias for the module 

For example,

import numpy as np

We can directly use np to call the numpy module.


Create Numpy Array

An array is 1 dimensional data structure used to store single data type data.

I.E It will only store all integer data or all string type data.or all float type data.

We can create an numpy array by using array() function.

Syntax:

numpy.array(elements)

where, elements are the input data elements.

 

Now lets move to sorting numpy array in python.


Scenario - 1 : Sorting numpy array without any parameters

Here, we are using sort function in numpy or sort() function to sort numpy array in ascending order.

Syntax:

numpy.sort(array_data)

where, array_data is the input numpy array.

 

Examplesorting numpy array in python

In this sorting numpy array example, we will sort numpy array without any parameters.

#importing the numpy module
import numpy 


#create an array with 8 elements - integer type 
array_data=numpy.array([34,56,43,22,45,6,54,2])

#actual array
print(array_data)

#sorted array
print(numpy.sort(array_data))

Outputsorting numpy array result

[34 56 43 22 45  6 54  2]
[ 2  6 22 34 43 45 54 56]

We can see this sort numpy array in ascending order. Now lets learn about sorting numpy array in descending order.


sorting numpy array in descending order

If we want to sort numpy array in python in descending order, we have to specify an slice operator - [::-1]

 

Examplesorting numpy array in descending order

In this sort numpy array in descending order example, we are sorting the numpy array in descending order.

#importing the numpy module
import numpy 


#create an array with 8 elements - integer type 
array_data=numpy.array([34,56,43,22,45,6,54,2])

#actual array
print(array_data)

#sorted array in dscending order
print(numpy.sort(array_data)[::-1])

Outputsort numpy array in descending order result

[34 56 43 22 45  6 54  2]
[56 54 45 43 34 22  6  2]

So this is how we can perform sorting numpy array in descending order.


Scenario 2  - Sort numpy array with different sorting algorithms

We are using same sort() function, but we will add the kind parameter, which will take the algorithms.

Syntax:

numpy.sort(array_data,kind)

where, array_data is the input numpy array.

 

Examplesorting numpy array

Sort numpy array in ascending order with different algorithms.

#importing the numpy module
import numpy 


#create an array with 8 elements - integer type 
array_data=numpy.array([34,56,43,22,45,6,54,2])

#actual array
print(array_data)

print()

#sorted array using mergesort alogorithm
print(numpy.sort(array_data,kind='mergesort'))

print()

#sorted array using quicksort alogorithm
print(numpy.sort(array_data,kind='quicksort'))

print()

#sorted array using heapsort alogorithm
print(numpy.sort(array_data,kind='heapsort'))

print()

#sorted array using stablesort alogorithm
print(numpy.sort(array_data,kind='stablesort'))

print()

#sorted array using selectionsort alogorithm
print(numpy.sort(array_data,kind='selectionsort'))

print()

Outputsort numpy array in ascending order result

From the above code, we used merge sort,quick sort, stable sort, selection sort and heap sorting algorithms.

[34 56 43 22 45  6 54  2]

[ 2  6 22 34 43 45 54 56]

[ 2  6 22 34 43 45 54 56]

[ 2  6 22 34 43 45 54 56]

[ 2  6 22 34 43 45 54 56]

[ 2  6 22 34 43 45 54 56]

This wraps up our session on sort numpy array in ascending order and sort numpy array in descending order using sort function in numpy.


Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 21,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!