This is the code for Quick sort in which i used assignment to swap the values.
def partion(arr,low,high):
partion_index=low
pivot=arr[partion_index]
while (low < high):
while low < len(arr) and (arr[low]<=pivot):
low+=1
while (arr[high]>=pivot):
high-=1
if (low < high):
arr[low],arr[high]=arr[high],arr[low]
pivot,arr[high]=arr[high],pivot
return high
def quickSort(arr,low,high):
if(low <high):
p=partion(arr,low,high)
quickSort(arr,low,p-1)
quickSort(arr,p+1,high)
This is producing the wrong results. When i tried using swap function for the same job it worked.
def swap(a, b, arr):
if a!=b:
tmp = arr[a]
arr[a] = arr[b]
arr[b] = tmp
def partion(arr,low,high):
partion_index=low
pivot=arr[partion_index]
while (low < high):
while low < len(arr) and (arr[low]<=pivot):
low+=1
while (arr[high]>=pivot):
high-=1
if (low < high):
swap(low, high, arr)
swap(partion_index, high, arr)
return high
Why is it so?
New contributor
izz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.