human
Give me a code to iterative Quick Sort and the other steps required given the following:
Iterative Quick Sort | This function is same in both iterative and recursive.
Function to do Quick sort arr [ ] -- > Array to be sorted , l -- > Starting index , h -- > Ending index.
Create an auxiliary stack.
initialize top of stack.
push initial values of l and h to stack.
Keep popping from stack while is not empty.
Pop h and l.
Set pivot element at its correct position in sorted array.
If there are elements on left side of pivot , then push left side to stack.
If there are elements on right side of pivot , then push right side to stack.
Driver code to test above.
Function calling"
How can the above be solved in Python?
assistant
#Here is code to iterative Quick Sort and the other steps required.
 def partition ( arr , l , h ) :
	i = ( l - 1 )
	x = arr [ h ]
	for j in range ( l , h ) :
		if arr [ j ] <= x :
			i = i + 1
			arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
	arr [ i + 1 ] , arr [ h ] = arr [ h ] , arr [ i + 1 ]
	return ( i + 1 )
def quickSortIterative ( arr , l , h ) :
	size = h - l + 1
	stack = [ 0 ] * ( size )
	top = - 1
	top = top + 1
	stack [ top ] = l
	top = top + 1
	stack [ top ] = h
	while top >= 0 :
		h = stack [ top ]
		top = top - 1
		l = stack [ top ]
		top = top - 1
		p = partition ( arr , l , h )
		if p - 1 > l :
			top = top + 1
			stack [ top ] = l
			top = top + 1
			stack [ top ] = p - 1
		if p + 1 < h :
			top = top + 1
			stack [ top ] = p + 1
			top = top + 1
			stack [ top ] = h
arr = [ 4 , 3 , 5 , 2 , 1 , 3 , 2 , 3 ]
n = len ( arr )
quickSortIterative ( arr , 0 , n - 1 )
print ( " Sorted ▁ array ▁ is : " )
for i in range ( n ) :
	print ( " % ▁ d " % arr [ i ] ) ,