Quicksort 2 - Sorting

  • + 0 comments

    Python

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    isize = input()
    inp = input()
    inp = inp.split(" ")
    arr = []
    for i in inp:
        arr.append(int(i))
    
    def quicksort(arr):
        if (len(arr)<=1):
            return arr
        fv = arr[0]
        left = []
        right = []
        equal = [fv]
        for i in range(1, len(arr)):
            if (arr[i]< fv):
                left.append(arr[i])
            elif (arr[i] > fv):
                right.append(arr[i])
            else:
                equal.append(arr[i])
        left = quicksort(left)
    
        right = quicksort(right)
        new_ord_arr = left+equal+right
        print(*new_ord_arr)
        return new_ord_arr
    
    quicksort(arr)