Python Program to Implement Quick Sorting

01. Example:

def quickSorting(data, start, end):
    
    if end - start > 1:
        p = partition(data, start, end)
        quickSorting(data, start, p)
        quickSorting(data, p + 1, end)
 
 
def partition(data, start, end):
    pivot = data[start]
    i = start + 1
    j = end - 1
 
    while True:
        while (i <= j and data[i] <= pivot):
            i = i + 1
        while (i <= j and data[j] >= pivot):
            j = j - 1
 
        if i <= j:
            data[i], data[j] = data[j], data[i]
        else:
            data[start], data[j] = data[j], data[start]
            return j
 
 
list01 = input('Enter the list of numbers: ').split()
list01 = [int(x) for x in list01]

quickSorting(list01, 0, len(list01))

print('nSorted list: ', end='')
print(list01)

Output:

Enter the list of numbers: 320 420 140 500 185 60 200 80 10 100
Sorted list: [10, 60, 80, 100, 140, 185, 200, 320, 420, 500]

 

Leave a comment