Question

In: Computer Science

Using PYTHON. Implement the stooge sort algorithm3to sort an array/vector of integers. Your program should be...

Using PYTHON. Implement the stooge sort algorithm3to sort an array/vector of integers.

Your program should be able to read inputs from a file called “data.txt” where the first value of each line is the number of integers that need to be sorted, followed by the integers

The output will be written to a file called “stooge.txt”.

Stooge sort is a “bad” recursive sorting algorithm. Given an array A, the algorithm can be defined as follows:

Step 1:

If the value at the leftmost position of the array is larger than the value at the rightmost position then swap values.

Step 2: If there are 3 or more elements in the array, then:

 Recursively call Stooge sort with the initial 2/3 of the array

 Recursively call Stooge sort with the last 2/3 of the array.

 Recursively call Stooge sort with the initial 2/3 of the array again.

Examplevalues for data.txt :

4 19 2 5 11

8 1 2 3 4 5 6 1 2

Solutions

Expert Solution

ANSWER:-

def stooge_sort(list, low, high):
if low >= high:
return
# if the value at left is larger then swap
if list[low] > list[high]:
temp = list[low]
list[low] = list[high]
list[high] = temp
# if there are 3 or more elements in the array
# recursively call stooge_sort with intial 2/3 of the array
# recursively call stooge_sort with last 2/3 of the array
# recursively call stooge_sort with intial 2/3 of the array
if high-low+1 > 2:
t = (int)((high - low + 1)/3)
stooge_sort(list, low, (high-t))
stooge_sort(list, low+t, (high))
stooge_sort(list, low, (high-t))
out_file = open("stooge.txt","w")
with open("data.txt") as f:
for line in f: # reads file line by line
l = []
dummy = line.split() # it splits line by space
for i in range(1,len(dummy)):
l.append(int(dummy[i])) # convert every element to int and append
stooge_sort(l,0,len(l)-1) # call sort
s = ''
for i in l:
s += str(i) + ' ' # convert elements into string to append into file
s += '\n'
print(s)
out_file.write(s)
out_file.close()

  
  

OUTPUT:-

NOTE:- If you have any doubts, please comment below. THANK YOU!!! Please give positive rating.


Related Solutions

// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Create a Python program that will take an unsorted list of 1000 integers and sort them...
Create a Python program that will take an unsorted list of 1000 integers and sort them using a bubble sort and an insertion sort. Your output should include displaying both sorted lists (from each algorithm) identifying each sorted list by the algorithm used.
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6,...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT