Iterative Quick Sort

Iterative Quick Sort

Following is an iterative implementation of the above recursive code from geekforgeek:

# Python program for implementation of Quicksort 
# This function is same in both iterative and recursive 
def partition(arr,l,h): 
   i = ( l - 1 ) 
   x = arr[h] 

   for j in range(l , h): 
   	if arr[j] <= x: 

   		# increment index of smaller element 
   		i = i+1
   		arr[i],arr[j] = arr[j],arr[i] 

   arr[i+1],arr[h] = arr[h],arr[i+1] 
   return (i+1) 

# Function to do Quick sort 
# arr[] --> Array to be sorted, 
# l --> Starting index, 
# h --> Ending index 
def quickSortIterative(arr,l,h): 

   # Create an auxiliary stack 
   size = h - l + 1
   stack = [0] * (size) 

   # initialize top of stack 
   top = -1

   # push initial values of l and h to stack 
   top = top + 1
   stack[top] = l 
   top = top + 1
   stack[top] = h 

   # Keep popping from stack while is not empty 
   while top >= 0: 

   	# Pop h and l 
   	h = stack[top] 
   	top = top - 1
   	l = stack[top] 
   	top = top - 1

   	# Set pivot element at its correct position in 
   	# sorted array 
   	p = partition( arr, l, h ) 

   	# If there are elements on left side of pivot, 
   	# then push left side to stack 
   	if p-1 > l: 
   		top = top + 1
   		stack[top] = l 
   		top = top + 1
   		stack[top] = p - 1

   	# If there are elements on right side of pivot, 
   	# then push right side to stack 
   	if p+1 < h: 
   		top = top + 1
   		stack[top] = p + 1
   		top = top + 1
   		stack[top] = h 

# Driver code to test above 
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]), 

# This code is contributed by Mohit Kumra 

My codes, read Function 3:

    def quickSorting(self,arry):
        def qsort_rec(arry,start,end):      
            if start >= end:
                return 
            i = start
            j = end
            pivot = arry[start]
            while i<j:
                while i<j and arry[j] >= pivot:
                    j -=1
                if i<j:
                    arry[i] = arry[j]
                    i +=1
                while i<j and arry[i] <= pivot:
                    i +=1
                if i<j:
                    arry[j] = arry[i]
                    j -=1
            arry[i] = pivot
            qsort_rec(arry,start,i-1)
            qsort_rec(arry,i+1,end)
            
        qsort_rec(arry,0,len(arry)-1)

    def quickSorting2(self,arry):
        def qsort_rec(arry,start,end):
            
            if start >= end:
                return
            
            i = start           
            for j in range(start+1,end+1):
                if arry[j] < arry[start]:
                    i +=1
                    arry[i],arry[j] = arry[j],arry[i]
            arry[i],arry[start] = arry[start],arry[i]
            qsort_rec(arry,start,i-1)
            qsort_rec(arry,i+1,end)
            
        qsort_rec(arry,0,len(arry)-1)
        
    def quickSorting3(self,arry):
        
        def partition(arry,start,end):
            i = start           
            for j in range(start+1,end+1):
                if arry[j] < arry[start]:
                    i +=1
                    arry[i],arry[j] = arry[j],arry[i]
            arry[i],arry[start] = arry[start],arry[i]
            return i
        
        stack =[0,len(arry)-1,]
        
        while stack:
            end = stack.pop()
            start = stack.pop()
            pivot = partition(arry,start,end)
            
            if start < pivot-1:
                stack += [start]
                stack += [pivot-1]
                
            if end > pivot +1:
                stack += [pivot+1]
                stack += [end]

Reference:

https://www.geeksforgeeks.org/iterative-quick-sort/

猜你喜欢

转载自blog.csdn.net/weixin_40759186/article/details/83473048