human
Rearrange Odd and Even values in Alternate Fashion in Ascending Order | Python3 implementation of the above approach.
Sort the array.
v1 = list ( ) to insert even values v2 = list ( ) to insert odd values.
Set flag to true if first element is even.
Start rearranging array.
If first element is even.
Else , first element is Odd.
Print the rearranged array.
Driver code"
Solution in Python:
assistant
#Here is code to rearrange Odd and Even values in Alternate Fashion in Ascending Order and the other steps required.
 def AlternateRearrange ( arr , n ) :
	arr . sort ( )
	for i in range ( n ) :
		if ( arr [ i ] % 2 == 0 ) :
			v1 . append ( arr [ i ] )
		else :
			v2 . append ( arr [ i ] )
	index = 0
	i = 0
	j = 0
	flag = False
	if ( arr [ 0 ] % 2 == 0 ) :
		flag = True
	while ( index < n ) :
		if ( flag == True and i < len ( v1 ) ) :
			arr [ index ] = v1 [ i ]
			index += 1
			i += 1
			flag = ~ flag
		elif j < len ( v2 ) :
			arr [ index ] = v2 [ j ]
			index += 1
			j += 1
			flag = ~ flag
	for i in range ( n ) :
		print ( arr [ i ] , end = " ▁ " )
arr = [ 9 , 8 , 13 , 2 , 19 , 14 , 21 , 23 , 25 ]
n = len ( arr )
AlternateRearrange ( arr , n )