
class  UpperCamelCase__		:







	'''simple docstring'''





	def __init__(			self				,       UpperCamelCase__		):
					A__  :	Optional[Any]         =							val
					A__  :	Any         =							None
					A__  :	Optional[Any]         =							None





	def   __snake_case				(			self				,       UpperCamelCase__		):

					if self.val:
									if val < self.val:
													if self.left is None:
																	A__  :	Any         =							Node(_lowercase		)
													else:
																	self.left.insert(_lowercase		)
									elif val > self.val:
													if self.right is None:
																	A__  :	str         =							Node(_lowercase		)
													else:
																	self.right.insert(_lowercase		)
					else:
									A__  :	Tuple         =							val




def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						Union[str, Any]							,						__UpperCamelCase :						List[Any]       ) ->       Tuple:

				"""simple docstring"""







				if root:
								inorder(root.left							,						_lowerCamelCase       )
								res.append(root.val       )
								inorder(root.right							,						_lowerCamelCase       )

def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						Optional[int]       ) ->       Tuple:

				"""simple docstring"""







				if len(_lowerCamelCase       ) == 0:
								return arr
				A__  :	Optional[int]         =							Node(arr[0]       )
				for i in range(1							,						len(_lowerCamelCase       )       ):
								root.insert(arr[i]       )
				# Traverse BST in order.
				A__  :	Tuple         =							[]
				inorder(_lowerCamelCase							,						_lowerCamelCase       )
				return res


if __name__ == "__main__":
					print(tree_sort([1_0, 1, 3, 2, 9, 1_4, 1_3]))







def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						int       ) ->       List[Any]:

				"""simple docstring"""







				A__  :	Optional[Any]         =							0
				A__  :	Optional[Any]         =							len(__UpperCamelCase       )

				for i in range(n - 1       ):
								for j in range(i + 1							,						__UpperCamelCase       ):
												if arr[i] > arr[j]:
																num_inversions += 1

				return num_inversions

def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						int       ) ->       Tuple:

				"""simple docstring"""







				if len(__UpperCamelCase       ) <= 1:
								return arr, 0
				A__  :	Optional[int]         =							len(__UpperCamelCase       ) // 2
				A__  :	List[str]         =							arr[0:mid]
				A__  :	Union[str, Any]         =							arr[mid:]

				A__       ,							A__  :	List[Any]         =							count_inversions_recursive(__UpperCamelCase       )
				A__       ,							A__  :	int         =							count_inversions_recursive(__UpperCamelCase       )
				A__       ,							A__  :	Dict         =							_count_cross_inversions(__UpperCamelCase							,						__UpperCamelCase       )

				A__  :	Any         =							inversion_p + inversions_q + cross_inversions
				return c, num_inversions

def 				SCREAMING_SNAKE_CASE     (      __UpperCamelCase :						List[str]							,						__UpperCamelCase :						List[Any]       ) ->       Dict:

				"""simple docstring"""







				A__  :	str         =							[]
				A__  :	Tuple         =							0
				while i < len(__UpperCamelCase       ) and j < len(__UpperCamelCase       ):
								if p[i] > q[j]:
												# if P[1] > Q[j], then P[k] > Q[k] for all  i < k <= len(P)
												# These are all inversions. The claim emerges from the
												# property that P is sorted.
												num_inversion += len(__UpperCamelCase       ) - i
												r.append(q[j]       )
												j += 1
								else:
												r.append(p[i]       )
												i += 1

				if i < len(__UpperCamelCase       ):
								r.extend(p[i:]       )
				else:
								r.extend(q[j:]       )

				return r, num_inversion

def 				SCREAMING_SNAKE_CASE     (      ) ->       Tuple:

				"""simple docstring"""







				A__  :	List[str]         =							[10, 2, 1, 5, 5, 2, 11]

				# this arr has 8 inversions:
				# (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2)

				A__  :	int         =							count_inversions_bf(__UpperCamelCase       )
				A__       ,							A__  :	int         =							count_inversions_recursive(__UpperCamelCase       )

				assert num_inversions_bf == num_inversions_recursive == 8

				print('''number of inversions = '''							,						__UpperCamelCase       )

				# testing an array with zero inversion (a sorted arr_1)

				arr_a.sort()
				A__  :	Optional[Any]         =							count_inversions_bf(__UpperCamelCase       )
				A__       ,							A__  :	Dict         =							count_inversions_recursive(__UpperCamelCase       )

				assert num_inversions_bf == num_inversions_recursive == 0
				print('''number of inversions = '''							,						__UpperCamelCase       )

				# an empty list should also have zero inversions
				A__  :	Union[str, Any]         =							[]
				A__  :	Union[str, Any]         =							count_inversions_bf(__UpperCamelCase       )
				A__       ,							A__  :	Any         =							count_inversions_recursive(__UpperCamelCase       )

				assert num_inversions_bf == num_inversions_recursive == 0
				print('''number of inversions = '''							,						__UpperCamelCase       )


if __name__ == "__main__":
					main()