human
Can you provide a code to reorder an array according to given indexes and the other steps required given the following:
Reorder an array according to given indexes | Python3 code to reorder an array according to given indices.
left child in 0 based indexing.
right child in 1 based indexing.
Find largest index from root , left and right child.
Swap arr whenever index is swapped.
Build heap.
Swap the largest element of index ( first element ) with the last element.
Swap arr whenever index is swapped.
Driver Code"
Solution in Python:
assistant
#Here is code to reorder an array according to given indexes and the other steps required.
 def heapify ( arr , index , i ) :
	largest = i
	left = 2 * i + 1
	right = 2 * i + 2
	global heapSize
	if ( left < heapSize and index [ left ] > index [ largest ] ) :
		largest = left
	if ( right < heapSize and index [ right ] > index [ largest ] ) :
		largest = right
	if ( largest != i ) :
		arr [ largest ] , arr [ i ] = arr [ i ] , arr [ largest ]
		index [ largest ] , index [ i ] = index [ i ] , index [ largest ]
		heapify ( arr , index , largest )
def heapSort ( arr , index , n ) :
	global heapSize
	for i in range ( int ( ( n - 1 ) / 2 ) , - 1 , - 1 ) :
		heapify ( arr , index , i )
	for i in range ( n - 1 , 0 , - 1 ) :
		index [ 0 ] , index [ i ] = index [ i ] , index [ 0 ]
		arr [ 0 ] , arr [ i ] = arr [ i ] , arr [ 0 ]
		heapSize -= 1
		heapify ( arr , index , 0 )
arr = [ 50 , 40 , 70 , 60 , 90 ]
index = [ 3 , 0 , 4 , 1 , 2 ]
n = len ( arr )
global heapSize
heapSize = n
heapSort ( arr , index , n )
print ( " Reordered ▁ array ▁ is : ▁ " )
print ( * arr , sep = ' ▁ ' )
print ( " Modified ▁ Index ▁ array ▁ is : ▁ " )
print ( * index , sep = ' ▁ ' )