Skip to main content

# NumPy Array Sorting

In this tutorial you will learn:

• What is array sorting?
• Sorting 1D and 2D arrays
• Sorting 3D arrays

Array Sorting

Array sorting is carried out to arrange the elements of the array in an ordered sequence. Array sorting may be carried out alphabetically or numerically. Array sorting facilitates and speeds up the binary array search. In NumPy array sorting is carried out using sort() function. The function can takes in one mandatory parameter which is the array itself that is to be sorted and 3 optional parameters which includes axis, searching algorithm and field. There are 4 types of algorithms that could be used in sorting arrays.

• Quicksort
• Mergesort
• Heapsort
• Stable

If the algorithm is not explicitly defined the functions by default takes the algorithm as quicksort. Note that both ‘stable’ and ‘mergesort’ use timsort or radix sort under the covers and, in general, the actual implementation will vary with data type. Stable algorithm was added in NumPy 1.15.0.

Sorting 1D and 2D arrays

In order to remain focused towards the basics of syntax and concept, we will only define the mandatory parameter.

In this example we will sort a 1D array to gain familiarity with the syntax. Here you can observe that the sort function takes in a single argument that is the array to be sorted.

`import numpy as npmy_arr = np.array([1,6,2,3,4,5,7])print('Array to be sorted is:',my_arr)#sorting arrayres = np.sort(my_arr)#printing resultprint('Result after sorting array:',res)`

Lets take another example, in this example we will sort a 2D array, here you can observe that that sort() function will be implied to each row locally. In this example you can see that there is a 0 in second row of this 2D array, since the array is in second row so it will be sorted in second row accordingly hence the sorting of each row is independent of the other rows.

`import numpy as npmy_arr = np.array([[2,3,1,5,4,6,7],[9,0,10,16,15,12,11]])print('2D Array to be sorted is:',my_arr)#sorting arrayres = np.sort(my_arr)#printing resultprint('Result after sorting 2D array:',res)`

In this example we will be sorting a 2D array of alphabets. Here you can observe that the sort() function works irrespective of the data type.

`import numpy as npmy_arr = np.array([['b','w','a','c'],['s','x','t','z']])print('2D Array to be sorted is:',my_arr)#sorting arrayres = np.sort(my_arr)#printing resultprint('Result after sorting 2D array:',res)`

In this example we will be sorting a 2D array of strings, in results you can observe that the sorting depends on each character of the string and as a result ‘sou’ was placed before ‘ster’ in results in first row of the array.

`import numpy as npmy_arr = np.array([['sou','rce','code','ster'],['num','py','tut','orials']])print('2D Array to be sorted is:',my_arr)#sorting arrayres = np.sort(my_arr)#printing resultprint('Result after sorting 2D array:',res)`

Sorting 3D arrays

In this example, we will be sorting a numeric 3D array using implicit parameters. In results you can observe that array is sorted row wise.

`import numpy as npmy_arr = np.array([[[6,7,3,2],[10,8,9,1],[15,11,13,12]]])print('3D Array to be sorted is:\n',my_arr)#sorting arrayres = np.sort(my_arr)#printing resultprint('Result after sorting 3D array: \n',res)`

Lets take another example, in this example we are sorting a 3D array of Data Type string and here you can observe sorting has taken place character wise.

`import numpy as npmy_arr = np.array([[['sou','rce','code','ster'],['num','py','tut','orials'],['are','fun','lear','ning']]])print('3D Array to be sorted is:\n',my_arr)#sorting arrayres = np.sort(my_arr)#printing resultprint('Result after sorting 3D array: \n',res)`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

## Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.