human
Iterative Quick Sort | Function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot.
The main function that implements QuickSort arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index Function to do Quick sort.
pi is partitioning index , arr [ p ] is now at right place.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to iterative Quick Sort and the other steps required.
 def partition ( arr , low , high ) :
	i = ( low - 1 )
	pivot = arr [ high ]
	for j in range ( low , high ) :
		if arr [ j ] <= pivot :
			i += 1
			arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
	arr [ i + 1 ] , arr [ high ] = arr [ high ] , arr [ i + 1 ]
	return ( i + 1 )
def quickSort ( arr , low , high ) :
	if low < high :
		pi = partition ( arr , low , high )
		quickSort ( arr , low , pi - 1 )
		quickSort ( arr , pi + 1 , high )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 4 , 2 , 6 , 9 , 2 ]
	n = len ( arr )
	quickSort ( arr , 0 , n - 1 )
	for i in range ( n ) :
		print ( arr [ i ] , end = " ▁ " )