Code:
Note:- Scroll horizontally to see the full line of code.
def swap(arr,i,j):
# takes arr as list, and swaps the element at i and j th postion of the arr
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
def partition(arr,start,end):
# return the index of the pivot element of arr
pivot = arr[end]
i=start-1
for j in range(start,end):
if(arr[j]<pivot):
i=i+1
swap(arr,i,j)
swap(arr,(i+1),end)
return (i+1)
def quick_sort(arr,start,end):
# takes arr as list to be sorted and start and end index of list arr
if (start<end):
pivot = partition(arr,start,end)
# print("Pivot element is:", arr[pivot])
print("Quick Sort: ",arr)
quick_sort(arr,start,(pivot-1))
quick_sort(arr,(pivot+1),end)
Comments
Post a Comment