Implement the following two sorting algorithms in a program called p3.py. Write two separate functions for these algorithms. Both functions must take a list of integers as the input parameter. 1) Bogosort: first shuffle the list argument (i.e., randomize the positions of every element) and then check to see if the result is in sorted order. If it is, the algorithm terminates successfully and returns True, but if it is not then the process must be repeated. 2) Bozosort: choose two elements in the list at random, swap them, and then check if the result is in sorted order. If it is, the algorithm terminates successfully and returns True, but if it is not then the process must be repeated. Write a main() function and call both sorting functions using the same list as their arguments. The list can be of any size (try a small list first). Does any of your algorithms terminate? If yes, count the number of iterations it uses to sort the list. Does it always use the same number of repetitions? If not, why? Discuss with the TAs and your peers.
Implement the following two sorting algorithms in a program called p3.py. Write two separate functions for these algorithms. Both functions must take a list of integers as the input parameter.
1) Bogosort: first shuffle the list argument (i.e., randomize the positions of every element) and then check to see if the result is in sorted order. If it is, the
2) Bozosort: choose two elements in the list at random, swap them, and then check if the result is in sorted order. If it is, the algorithm terminates successfully and returns True, but if it is not then the process must be repeated.
Write a main() function and call both sorting functions using the same list as their arguments. The list can be of any size (try a small list first). Does any of your algorithms terminate? If yes, count the number of iterations it uses to sort the list. Does it always use the same number of repetitions? If not, why? Discuss with the TAs and your peers.
Step by step
Solved in 6 steps with 4 images