Question

Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive...

Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive quick sort, I need to make it non-recursive, any help is appreciated!

def quickSort(list):
    quickSortHelper(list, 0, len(list) - 1)

def quickSortHelper(list, first, last):
    if last > first:
        pivotIndex = partition(list, first, last)
        quickSortHelper(list, first, pivotIndex - 1)
        quickSortHelper(list, pivotIndex + 1, last)

# Partition list[first..last]
def partition(list, first, last):
    pivot = list[first] # Choose the first element as the pivot
    low = first + 1 # Index for forward search
    high = last # Index for backward search

    while high > low:
        # Search forward from left
        while low <= high and list[low] <= pivot:
            low += 1

        # Search backward from right
        while low <= high and list[high] > pivot:
            high -= 1

        # Swap two elements in the list
        if high > low:
            list[high], list[low] = list[low], list[low]

    while high > first and list[high] >= pivot:
        high -= 1

    # Swap pivot with list[high]
    if pivot > list[high]:
        list[first] = list[high]
        list[high] = pivot
        return high
    else:
        return first

# A test function
def main():
    list = [2, 3, 2, 5, 6, 1, -2, 3, 14, 12]
    quickSort(list)
    for v in list:
        print(str(v) + " ", end = "")

main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code is given below: (modified code segment is highlighted)

def quickSort(list):
    quickSortHelper(list, 0, len(list) - 1)

def quickSortHelper(list, first, last):
    if last > first:
        pivotIndex = partition(list, first, last)
        quickSortHelper(list, first, pivotIndex - 1)
        quickSortHelper(list, pivotIndex + 1, last)

# Partition list[first..last]
def partition(list, first, last):
    pivot = list[first] # Choose the first element as the pivot
    low = first + 1 # Index for forward search
    high = last # Index for backward search

    while high > low:
        # Search forward from left
        while low <= high and list[low] <= pivot:
            low += 1

        # Search backward from right
        while low <= high and list[high] > pivot:
            high -= 1

        # Swap two elements in the list
        if high > low:
            temp = list[high]
            list[high] = list[low]
            list[low] = temp

    while high > first and list[high] > pivot:
        high -= 1

    # Swap pivot with list[high]
    if pivot > list[high]:
        list[first] = list[high]
        list[high] = pivot
        return high
    else:
        return first


def quickSortIterative(list):
    l = 0 # set the low and high
    h = len(list) - 1
    size = h - l + 1  
    st = [0] * (size)  # initializing the stack
    top = -1 # it represents the empty stack
    top = top + 1
    st[top] = l # inserting low and high in stack
    top = top + 1
    st[top] = h
    while top >= 0: # run while loop until the stack becomes empty
        h = st[top] # popping the low and high
        top = top - 1
        l = st[top]
        top = top - 1
        p = partition(list, l, h) # partition 
        if p - 1 > l: # again push the range low to pivot-1
            top = top + 1
            st[top] = l
            top = top + 1
            st[top] = p - 1
        if p + 1 < h:  # push the range pivot + 1 to high
            top = top + 1
            st[top] = p + 1
            top = top + 1
            st[top] = h

# A test function
def main():
    list = [2, 3, 2, 5, 6, 1, -2, 3, 14, 12]
    quickSortIterative(list)
    for v in list:
        print(str(v) + " ", end = "")

main()

Sample Output:
-2 1 2 2 3 3 5 6 12 14

Screenshot of the code is given below:

If the answer helped please upvote, it means a lot and for any query please comment.

Add a comment
Know the answer?
Add Answer to:
Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT