human
Sort an array according to absolute difference with a given value " using ▁ constant ▁ extra ▁ space " | Python 3 program to sort an array based on absolute difference with a given value x ..
Below lines are similar to insertion sort.
Insert arr [ i ] at correct place.
Function to print the array.
Driver Code"
Solution in Python:
assistant
#Here is code to sort an array according to absolute difference with a given value " using ▁ constant ▁ extra ▁ space " and the other steps required.
 def arrange ( arr , n , x ) :
	for i in range ( 1 , n ) :
		diff = abs ( arr [ i ] - x )
		j = i - 1
		if ( abs ( arr [ j ] - x ) > diff ) :
			temp = arr [ i ]
			while ( abs ( arr [ j ] - x ) > diff and j >= 0 ) :
				arr [ j + 1 ] = arr [ j ]
				j -= 1
			arr [ j + 1 ] = temp
def print_1 ( arr , n ) :
	for i in range ( n ) :
		print ( arr [ i ] , end = " ▁ " )
if __name__ == " _ _ main _ _ " :
	arr = [ 10 , 5 , 3 , 9 , 2 ]
	n = len ( arr )
	x = 7
	arrange ( arr , n , x )
	print_1 ( arr , n )