Quick Sort

#!/usr/bin/evn python

# Functiont to sort from both end
def sorting(list, start, end):
    pivot = list[end]
    bottom = start-1
    top = end

    i = 0
    while not i:

        while not i:
            bottom = bottom + 1

            if bottom == top:
                i = 1
                break

            if list[bottom] > pivot:
                list[top] = list[bottom]
                break

        while not i:
            top = top - 1

            if top == bottom:
                i = 1
                break

            if list[top] < pivot:
                list[bottom] = list[top]
                break

    list[top] = pivot
    return top

# Function for quick sort
def quickSort(list, start, end):
    if start < end:
        # Call function 'sorting'
        split = sorting(list, start, end)
        # Recursively call the same function
        quickSort(list, start, split-1)
        quickSort(list, split+1, end)
    else:
        return

numList = [5,8,1,6,3,7,2,4,9]
print('Before sort:')
print(numList)
# Calling 'quickSort' function by passing number array
quickSort(numList, 0, len(numList)-1)
print('After sort:')
print(numList)


# ***** Output *****
Before sort:
[5, 8, 1, 6, 3, 7, 2, 4, 9]
After sort:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!