Directly on the code:
#Quick Sort#Coding:utf-8defquicksort (a,left,right):if(left<Right ): Mid=partition (A,left,right) quicksort (A,left,mid-1) quicksort (A,mid+1, right)defpartition (A,left,right): x=A[right] I= Left-1#Initial I points to an empty, guaranteeing 0 to I are less than or equal to x forJinchRange (Left,right):#J is used to look for smaller than X, to find and i+1 exchange, to ensure that I was less than or equal to x if(a[j]<=x): I= I+1A[i],a[j]=A[j],a[i] A[i+1],a[right] = a[right],a[i+1]#0 to I are less than or equal to X, so the final position of X is i+1 returnI+1 while(True):Try: S= Input ("enter the array to be sorted: \ n")#array to be queuedL =S.split () a= [Int (t) forTinchl] Quicksort (A,0,len (a)-1) Print("after sorting:") forIteminchA:Print(item,end=' ') Print("\ n") except: Break
Random Quick sort:
#Random Quick Sort#Coding:utf-8ImportRandomdefRandom_quicksort (a,left,right):if(left<Right ): Mid=random_partition (a,left,right) random_quicksort (A,left,mid-1) Random_quicksort (A,mid+1, right)defrandom_partition (a,left,right): t= Random.randint (left,right)#generate a random number between [Left,right]A[t],a[right] =A[right],a[t] x=A[right] I= Left-1#Initial I points to an empty, guaranteeing 0 to I are less than or equal to x forJinchRange (Left,right):#J is used to look for smaller than X, to find and i+1 exchange, to ensure that I was less than or equal to x if(a[j]<=x): I= I+1A[i],a[j]=A[j],a[i] A[i+1],a[right] = a[right],a[i+1]#0 to I are less than or equal to X, so the final position of X is i+1 returnI+1 while(True):Try: S= Input ("enter the array to be sorted: \ n")#array to be queuedL =S.split () a= [Int (t) forTinchl] Random_quicksort (A,0,len (a)-1) Print("after sorting:") forIteminchA:Print(item,end=' ') Print("\ n") except: Break
Python for fast sorting and random quick sorting