Program to implement Quick sort in python

Program to implement Quick sort in python

In the python programming article, we are going to learn

  • program to implement quick sort in python

Program to implement quick sort in Python

The program is as follows:


# Owner : TutorialsInhand Author : Devjeet Roy

def make_partition(arr, l, h): 
    i = (l-1)          
    pivot = arr[h]     
    for j in range(l, h):  
        if arr[j] <= pivot: 
            i = i+1
            arr[i], arr[j] = arr[j], arr[i] 
    arr[i+1], arr[h] = arr[h], arr[i+1] 
    return (i+1) 

def quick_sort(arr,l,h):
    if len(arr) == 1: 
        return arr 
    if l < h: 
        pi = make_partition(arr, l, h) 
        quick_sort(arr, l, pi-1) 
        quick_sort(arr, pi+1, h) 
    return arr

if __name__ == "__main__":
    marks = [22,66,43,58,98,42,77,56,66]
    result = quick_sort(marks,0,len(marks)-1)

    print("The sorted marks: ", result)

The output is as follows:


PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python
The sorted marks:  [22, 42, 43, 56, 58, 66, 66, 77, 98]

Few important tips about the program

1. Quick sort follows divide and conquer paradigm of programming.

2. It picks an element as pivot and partitions the given array around the picked pivot.


quick sort in python


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by

About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!