Quick Sort:

def Partition(A,start,end): ##returns an array where elements to the left of pivot are less than the pivot and to the right are greater than the pivot
    pivot = A[end]
    pIndex = start
    for i in range(start,end):
        if A[i]<=pivot:
            temp = A[i]
            A[i] = A[pIndex]
            A[pIndex] = temp
            pIndex+=1

    temp = A[pIndex]
    A[pIndex] = A[end]
    A[end] = temp
    return pIndex

def QuickSort(A,start,end):
    if start<end:  ## Makes sure that there are more than one element in the array
        pIndex = Partition(A,start,end)
        QuickSort(A,start,pIndex-1)
        QuickSort(A,pIndex+1,end)


A=[1,3,9,8,2,7,5]
QuickSort(A,0,6)

print A

results matching ""

    No results matching ""