Running Time of Quicksort

  • + 0 comments

    Pithon 3

    def runningTime(ar):
        counter = 0
        for j in range(len(ar)):
            for i in range(j, len(ar)):
                if ar[j] > ar[i]:
                    counter += 1
        return counter
    
    def Quicksort_In_Place(ar, start, end, counter=0):
        if end - start > 1:
            a = ar[end-1]
            s = start
            for i in range(start, end):
                if a >= ar[i]:
                    ar[s], ar[i] = ar[i], ar[s]
                    s += 1
                    counter += 1
            counter = Quicksort_In_Place(ar, start, s-1, counter)
            counter = Quicksort_In_Place(ar, s, end, counter)
        return counter
    
    n = int(input())
    ar = list(map(int, input().split()))
    print(runningTime(ar) - Quicksort_In_Place(ar, 0, n))