human
Merge two sorted arrays in O ( 1 ) extra space using QuickSort partition | Function to perform the partition around the pivot element.
Stores index of each element of the array , arr [ ].
Stores index of each element of the array , brr [ ].
Traverse both the array.
If pivot is smaller than arr [ l ].
If Pivot is greater than brr [ r ].
If either arr [ l ] > Pivot or brr [ r ] < Pivot.
Function to merge the two sorted array.
Stores index of each element of the array arr [ ].
Stores index of each element of the array brr [ ].
Stores index of each element the final sorted array.
Stores the pivot element.
Traverse both the array.
If pivot element is not found or index < N.
If pivot element is not found or index < N.
Place the first N elements of the sorted array into arr [ ] and the last M elements of the sorted array into brr [ ].
Sort both the arrays.
Print the first N elements in sorted order.
Print the last M elements in sorted order.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to merge two sorted arrays in O ( 1 ) extra space using QuickSort partition and the other steps required.
 def partition ( arr , N , brr , M , Pivot ) :
	l = N - 1
	r = 0
	while ( l >= 0 and r < M ) :
		if ( arr [ l ] < Pivot ) :
			l -= 1
		elif ( brr [ r ] > Pivot ) :
			r += 1
		else :
			arr [ l ] , brr [ r ] = brr [ r ] , arr [ l ]
			l -= 1
			r += 1
def Merge ( arr , N , brr , M ) :
	l = 0
	r = 0
	index = - 1
	Pivot = 0
	while ( index < N and l < N and r < M ) :
		if ( arr [ l ] < brr [ r ] ) :
			Pivot = arr [ l ]
			l += 1
		else :
			Pivot = brr [ r ]
			r += 1
		index += 1
	while ( index < N and l < N ) :
		Pivot = arr [ l ]
		l += 1
		index += 1
	while ( index < N and r < M ) :
		Pivot = brr [ r ]
		r += 1
		index += 1
	partition ( arr , N , brr , M , Pivot )
	arr = sorted ( arr )
	brr = sorted ( brr )
	for i in range ( N ) :
		print ( arr [ i ] , end = " ▁ " )
	for i in range ( M ) :
		print ( brr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 5 , 9 ]
	brr = [ 2 , 4 , 7 , 10 ]
	N = len ( arr )
	M = len ( brr )
	Merge ( arr , N , brr , M )