Quick Sort in DSA Python

Program 1

#Program for Quick Sort
import os
def quick_sort(mylist,l,h):
    low=l
    high=h
    key=mylist[(low+high)//2]  # Pivot element
    while(low<=high):
         while(key>mylist[low]):
                 low=low+1
         while(key<mylist[high]):                  
                    high=high-1
         if(low<=high):
               mylist[low],mylist[high]=mylist[high],mylist[low]
               low=low+1
               high=high-1
    if(l<=high):
          quick_sort(mylist,l,high)
    if(low<h):
          quick_sort(mylist,low,h)
                                       

#Main Method
os.system('cls')
mylist=[]
n=int(input("Enter the limit"))
print("Enter an element")
for i in range(n):
    x=int(input())
    mylist.append(x)
low=0
high=n-1
quick_sort(mylist,low,high)

print("-------------Sorted element---------")
for i in range(n):
    print(mylist[i])

 

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *