We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# Enter your code here. Read input from STDIN. Print output to STDOUTdefrunningTimeIn(arr):count=0foriinrange(1,len(arr)):j=i-1key=arr[i]while(j>=0)and(arr[j]>key):arr[j+1]=arr[j]j-=1count+=1arr[j+1]=keyreturncountdefpartition(arr,low,high,swap):i=(low-1)pivot=arr[high]#swaps=0forjinrange(low,high):ifarr[j]<=pivot:i=i+1arr[i],arr[j]=arr[j],arr[i]swap+=1arr[i+1],arr[high]=arr[high],arr[i+1]swap+=1returni+1,swapdefrunningTimeQ(arr,low,high,swap=0):pi=0iflow<high:pi,swap=partition(arr,low,high,swap)#print(' '.join(map(str,arr)))swap=runningTimeQ(arr,low,pi-1,swap)swap=runningTimeQ(arr,pi+1,high,swap)returnswapif__name__=='__main__':n=int(input())arr=list(map(int,input().rstrip().split()))arrCopy=[]arrCopy.extend(arr)qRun=runningTimeQ(arr,0,n-1)inRun=runningTimeIn(arrCopy)result=inRun-qRunprint(result)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Running Time of Quicksort
You are viewing a single comment's thread. Return to all comments →