Articles

# 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 code.py
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.

Basic Python Programs

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