Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Numpy
    Tensorflow
    Interview Questions & Answers

    numpy.sort

    Numpy.sort() is a sorting function that helps in arranging the elements of an array in a certain order. Let’s say we want to sort our array in ascending order along the first axis, then numpy.sort() comes to our rescue. Numpy provides four different sorting algorithms: quicksort, heapsort, mergesort, and timsort. We will be discussing these algorithms in great detail while solving some examples. We can choose any of the mentioned algorithms based on the required time and space complexity.

    Syntax and Parameters:

    Moving ahead, let’s discuss the numpy.sort() function in greater details. The standard syntax for writing this function is as follows:

    numpy.sort(a, axis=-1, kind=None, order=None)

    The parameters of numpy.sort() are :

    • a: array-like object –

      The input array to be sorted.

    • axis: 0, -1 or none –

      The axis along which the array has to be sort. If nothing is mentioned, then the array is flattened before sorting. By default, the array is sorted along the last axis. It is denoted by axis = -1.

    • kind: {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’} –

      In numpy, we can choose from any of these sorting algorithms {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}. By default, the input array will be sorted by using quicksort.

    • order: order of sorting {string or list} –

      This parameter specifies the field along which the sorting has to be done. It usually used when we have a field defined array.

    Out of the above-mentioned parameters, the first parameter is compulsory. The other parameters are optional and can be used based on the specific requirement. The numpy.sort() function returns a sorted copy of the input array.

    Examples to Illustrate numpy.sort

    Following are the different examples to illustrate numpy.sort.

    Example #1

    Program to illustrate sorting along different axes using numpy.sort()

    Code:

    import numpy as np
    #creating an array
    A = np.array([[15, 1], [19, 94]])
    print ("The input array is : \n", A)
    # sorting along the first axis
    A_sorted = np.sort(A, axis = 0)
    print ("Sorted array along the first axis : \n", A_sorted)
    #sorting along the last axis
    A_sorted = np.sort(A, axis = -1)
    print ("Sorted array along the last axis : \n", A_sorted)
    #sorting the flattened axis
    A_sorted = np.sort(A, axis = None)
    print ("Sorted array when flattened: \n", A_sorted)
    

    Example #2

    Program to illustrate sorting using different sorting algorithms using numpy.sort()

    Code:

    import numpy as np
    #creating an array
    A = np.array([[19, 3], [19, 94]])
    print ("The input array is : \n", A)
    # sorting along the first axis using quicksort
    A_sorted = np.sort(A, axis = 0, kind = 'quicksort')
    print ("Sorted array using quicksort : \n", A_sorted)
    # sorting along the first axis using mergesort
    A_sorted = np.sort(A, axis = 0, kind = 'mergesort')
    print ("Sorted array using mergesort : \n", A_sorted)
    # sorting along the first axis using heapsort
    A_sorted = np.sort(A, axis = 0, kind = 'heapsort')
    print ("Sorted array using heapsort : \n", A_sorted)
    # sorting along the first axis using stable
    A_sorted = np.sort(A, axis = 0, kind = 'stable')
    print ("Sorted array using stable : \n", A_sorted)
    


     

    Apply now for Advanced Python Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.