human
Sort an Array which contain 1 to N values in O ( N ) using Cycle Sort | Function to print array element.
Traverse the array.
Function to sort the array in O ( N ).
Traverse the array.
If the current element is at correct position.
Else swap the current element with it 's correct position.
Swap the value of arr [ i ] and arr [ arr [ i ] - 1 ].
Driver code.
Function call to sort the array.
Function call to print the array"
Solution in Python:
 Write me a code to sort an Array which contain 1 to N values in O ( N ) using Cycle Sort and the other steps required?
assistant
#Here is code to sort an Array which contain 1 to N values in O ( N ) using Cycle Sort and the other steps required.
 def printArray ( arr , N ) :
	for i in range ( N ) :
		print ( arr [ i ] , end = ' ▁ ' )
def sortArray ( arr , N ) :
	i = 0
	while i < N :
		if arr [ i ] == i + 1 :
			i += 1
		else :
			temp1 = arr [ i ]
			temp2 = arr [ arr [ i ] - 1 ]
			arr [ i ] = temp2
			arr [ temp1 - 1 ] = temp1
if __name__ == ' _ _ main _ _ ' :
	arr = [ 2 , 1 , 5 , 3 , 4 ]
	N = len ( arr )
	sortArray ( arr , N )
	printArray ( arr , N )