Python ProgrammingPython Programming

Quick Sort

##
# Python's algorithm of Quick sort


def quick_sort(num_list):
    less = []
    pivot_list = []
    more = []
    if len(num_list) <= 1:
        return num_list
    else:
        pivot = num_list[0]
        for i in num_list:
            if i < pivot:
                less.append(i)
            elif i > pivot:
                more.append(i)
            else:
                pivot_list.append(i)
        less = quick_sort(less)
        more = quick_sort(more)
        return less + pivot_list + more


num_list = [75, 16, 55, 19, 48, 14, 2, 61, 22, 100]
print("Before: ", num_list)
num_list = quick_sort(num_list)
print("After:  ", num_list)

Sample output of above program.
C:\python\examples>pycodestyle --first quicksort.py

C:\python\examples>python cylesort.py
Before: [75, 16, 55, 19, 48, 14, 2, 61, 22, 100]
After: [2, 14, 16, 19, 22, 48, 55, 61, 75, 100]

C:\python\examples>