Python Programming

Quick Sort

Python programming for sorting numbers using quick sort algorithm

8/17/2021
0 views
quick-sort.pyPython
#!/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)
Pythonquick sortsort numbersortingsorting algorithm

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com