Search code examples
pythonarraysnumpysortingquicksort

quick sort algorithm by python


I tried to write some line of code for quick sort algorithm using numpy algorithm. But It seems not working properly . Can you help me solve it ?

import numpy as np
def quick_sort_np(narr):
    """A numpy version of quick sort algorithm"""
    narr = np.array(narr)
    if len(narr)<= 1 :
        return narr
    p = narr[-1] # I take the last item as pivot by convension
    print (f"at this level the pivot is {p}")
    lnarr = narr[narr<p]
    print (f"----------------------> left arr is {lnarr}")
    rnarr = narr[narr>p]
    print (f"----------------------> right arr is {rnarr}")
    return quick_sort_np(lnarr) + p + quick_sort_np(rnarr) 

in case of [1,2,6,5,4,8,7,99,33] as input my code returns nothing and that's the question.


Solution

  • + acting on np.arrays is element wise addition, not concatenation.