Question

In: Computer Science

Write a program in python that implements quicksort, first using recursion and then without recursion.

Write a program in python that implements quicksort, first using recursion and then without recursion.

Solutions

Expert Solution

Using Recursion:

def make_partition(arr, low, high):
        i = (low-1)              # index of smaller element
        temp = arr[high]

        for j in range(low, high):
                # If current element is smaller than or equal to temp then swap
    #element and increment i by 1
                if arr[j] <= temp:
                        i = i+1
                        arr[i], arr[j] = arr[j], arr[i] #swap element

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

def quickSort(arr, low, high):
  #check if array has only one element the return array
        if len(arr) == 1:
                return arr
        if low < high:
                #fnd partitioning_index by calling function make_partition()
                partitioning_index = make_partition(arr, low, high)

                #sort elements before partition and after partition
                quickSort(arr, low, partitioning_index-1)
                quickSort(arr, partitioning_index+1, high)

#empty array to be sorted
arr = []

#get number of element from user
size = int(input("Enter how many element you want to sort : "))

#get one by one element from user 
for i in range(1,size+1):
  element = int(input("Enter element of "+str(i)+" : "))
  arr.append(element)

#call function to sort array
quickSort(arr, 0, size-1)

#print sorted array
print(("-"*20)+" Sorted Array "+("-"*20))

for i in range(size):
        print("%d" % arr[i])

Output:

Without Recursion:

def partition(arr,l,h): 
    i = ( l - 1 ) 
    x = arr[h] 
  
    for j in range(l , h):
        # If current element is smaller than or equal to temp then swap
        #element and increment i by 1 
        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 quickSort(arr,l,h): 
  
    # Create stack 
    size = h - l + 1
    stack = [0] * (size) 
  
    # initialize top of stack 
    top = -1
  
    # push 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 middle element at its correct position in 
        # sorted array 
        middle = partition( arr, l, h ) 
  
        # If there are elements on left side of middle, 
        # then push left side to stack 
        if middle-1 > l: 
            top = top + 1
            stack[top] = l 
            top = top + 1
            stack[top] = middle - 1
  
        # If there are elements on right side of middle, 
        # then push right side to stack 
        if middle+1 < h: 
            top = top + 1
            stack[top] = middle + 1
            top = top + 1
            stack[top] = h 
  


#empty array to be sorted
arr = []

#get number of element from user
size = int(input("Enter how many element you want to sort : "))

#get one by one element from user 
for i in range(1,size+1):
  element = int(input("Enter element of "+str(i)+" : "))
  arr.append(element)

#call function to sort array
quickSort(arr, 0, size-1)

#print sorted array
print(("-"*20)+" Sorted Array "+("-"*20))

for i in range(size):
        print("%d" % arr[i])

Output:


Related Solutions

Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter...
PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter a word or empty string to finish: poster The word poster has the following 6 anagrams: presto repost respot stoper topers tropes
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Write a program, using any language you want, to sort the following using QuickSort: {3, 4,...
Write a program, using any language you want, to sort the following using QuickSort: {3, 4, 5,1, 2, 6, 7, 8}
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
Write a c program to calculate the factorial of a number using recursion    [8] Question five...
Write a c program to calculate the factorial of a number using recursion    [8] Question five Write a stack algorithm to POP an item                                                         [6] What does FRONT and REAR signify in a queue?                                                                 [6] Write an algorithm for a dequeue operation                                                                       [8]
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a sample input from a user. 3 2 0 1 1 2 The first line (= 3 in the example) indicates that there are three vertices in the graph. You can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Using Python write a function that implements the following two-dimensional objective function: F (x, y) =...
Using Python write a function that implements the following two-dimensional objective function: F (x, y) = (x^2 + y − 11)^2 + (x + y^2 − 7 )^22 . Determine how many local minimums and maximums and their location for this objective function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT