human
Give me a code to merge two sorted arrays in O ( 1 ) extra space using Heap and the other steps required given the following:
Merge two sorted arrays in O ( 1 ) extra space using Heap | Function to perform min heapify on array brr [ ].
Stores index of left child of i ..
Stores index of right child of i ..
Stores index of the smallest element in ( arr [ i ] , arr [ left ] , arr [ right ] ).
Check if arr [ left ] less than arr [ smallest ].
Update smallest.
Check if arr [ right ] less than arr [ smallest ].
Update smallest.
If i is not the index of smallest element.
Swap arr [ i ] and arr [ smallest ].
Perform heapify on smallest.
Function to merge two sorted arrays.
Traverse the array arr [ ].
Check if current element is less than brr [ 0 ].
Swap arr [ i ] and brr [ 0 ].
Perform heapify on brr [ ].
Sort array brr [ ].
Function to print array elements.
Traverse array arr [ ].
Driver code.
Function call to merge two array.
Print first array.
Print Second array ."
Solution in Python:
assistant
#Here is code to merge two sorted arrays in O ( 1 ) extra space using Heap and the other steps required.
 def minHeapify ( brr , i , M ) :
	left = 2 * i + 1
	right = 2 * i + 2
	smallest = i
	if ( left < M and brr [ left ] < brr [ smallest ] ) :
		smallest = left
	if ( right < M and brr [ right ] < brr [ smallest ] ) :
		smallest = right
	if ( smallest != i ) :
		brr [ i ] , brr [ smallest ] = brr [ smallest ] , brr [ i ]
		minHeapify ( brr , smallest , M )
def merge ( arr , brr , N , M ) :
	for i in range ( N ) :
		if ( arr [ i ] > brr [ 0 ] ) :
			arr [ i ] , brr [ 0 ] = brr [ 0 ] , arr [ i ]
			minHeapify ( brr , 0 , M )
	brr . sort ( )
def printArray ( arr , N ) :
	for i in range ( N ) :
		print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 2 , 23 , 35 , 235 , 2335 ]
	brr = [ 3 , 5 ]
	N = len ( arr )
	M = len ( brr )
	merge ( arr , brr , N , M )
	printArray ( arr , N )
	printArray ( brr , M )