Answers for "python quicksort built in"

1

quicksort code in python

#this function is for getting correct position of pivot
def pivot_place(list1,first,last):
    pivot = list1[first]
    left = first + 1
    right = last

    while True :

        while left <= right and  list1[left] <= pivot :
            left += 1
        while left<=right and list1[right] >= pivot:
            right -= 1

        if right < left :
            break
        else:
            list1[left],list1[right] = list1[right],list1[left]
    list1[first],list1[right] = list1[right],list1[first]

    return right
def quicksort(list1,first,last):
    if first < last :
        p = pivot_place(list1,first,last)
        quicksort(list1,first,p-1)
        quicksort(list1,p+1,last)
n = int(input("How many elements you want : "))
list1 = [int(input("Enter elements : ")) for x in range(n)]
quicksort(list1,0,n - 1)
print(list1)
Posted by: Guest on March-12-2021

Python Answers by Framework

Browse Popular Code Answers by Language