Question

In: Computer Science

Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......

Implement the following pseudocode in Python

Consider the following pseudocode for a sorting algorithm:

STOOGESORT(A[0 ... n - 1])
if (n = 2) and A[0] > A[1]
swap A[0] and A[1]
else if n > 2
m = ceiling(2n/3)
STOOGESORT(A[0 ... m - 1])
STOOGESORT(A[n - m ... n - 1])
STOOGESORT(A[0 ... m - 1])

Solutions

Expert Solution

CODE -

import math
def STOOGESORT(A):
n = len(A)
# Swapping the elements if length of list is 2 and first element is greater than second element.
if n == 2 and A[0]>A[1]:
temp = A[0]
A[0] = A[1]
A[1] = temp
# If length of list is greater than 2
elif n>2:
m = math.ceil(2 * n / 3)
# Recursively sorting the first 2/3 elements
A[0 : m] = STOOGESORT(A[0 : m])
# Recursively sorting the last 2/3 elements
A[n-m : ] = STOOGESORT(A[n-m : ])
# Recursively sorting the first 2/3 elements to confirm
A[0 : m] = STOOGESORT(A[0 : m])
return A

SCREENSHOT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
Develop an algorithm and implement Multilevel Queuing scheduling using C++.and using bubble sorting. Consider a set...
Develop an algorithm and implement Multilevel Queuing scheduling using C++.and using bubble sorting. Consider a set of user and system processes and determine (i) turnaround time (ii) waiting time of each process (iii) Average Waiting Time (AWT) and (iv) Average Turnaround Time (ATAT) of all processes. please write comment
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of your implementation is O(m)
Write a Python program that: Create the algorithm in both flowchart and pseudocode forms for the...
Write a Python program that: Create the algorithm in both flowchart and pseudocode forms for the following requirements: Reads in a series of positive integers,  one number at a time;  and Calculate the product (multiplication) of all the integers less than 25,  and Calculate the sum (addition) of all the integers greater than or equal to 25. Use 0 as a sentinel value, which stops the input loop. [ If the input is 0 that means the end of the input list. ]...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and...
1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in...
1.) (a) There exists a very popular sorting algorithm called Timsort, the default sorting algorithm in both Python and Java. This sort is a combination of two different sorting algorithms: Merge sort, and Insertion sort. Recall that the Big-O of Merge sort is O(nlogn) and the Big-O of Insertion sort is O(n 2 ). What advantage would Timsort have to combine the two algorithms if merge-sort has a better Big-O metric? (b) Consider two algorithms: f(n) and g(n). You run...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT