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.

  1. import numpy as np
  2. my_arr = np.array([1,6,2,3,4,5,7])
  3. print('Array to be sorted is:',my_arr)
  4. #sorting array
  5. res = np.sort(my_arr)
  6. #printing result
  7. print('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.

  1. import numpy as np
  2. my_arr = np.array([[2,3,1,5,4,6,7],[9,0,10,16,15,12,11]])
  3. print('2D Array to be sorted is:',my_arr)
  4. #sorting array
  5. res = np.sort(my_arr)
  6. #printing result
  7. print('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.

  1. import numpy as np
  2. my_arr = np.array([['b','w','a','c'],['s','x','t','z']])
  3. print('2D Array to be sorted is:',my_arr)
  4. #sorting array
  5. res = np.sort(my_arr)
  6. #printing result
  7. print('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.

  1. import numpy as np
  2. my_arr = np.array([['sou','rce','code','ster'],['num','py','tut','orials']])
  3. print('2D Array to be sorted is:',my_arr)
  4. #sorting array
  5. res = np.sort(my_arr)
  6. #printing result
  7. print('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.

  1. import numpy as np
  2. my_arr = np.array([[[6,7,3,2],[10,8,9,1],[15,11,13,12]]])
  3. print('3D Array to be sorted is:\n',my_arr)
  4. #sorting array
  5. res = np.sort(my_arr)
  6. #printing result
  7. print('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.

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

Add new comment