Task 1: Merge Sort Part A: Merge Write a function merge (1st1, 1st2) that takes two...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Task 1: Merge Sort Part A: Merge Write a function merge (1st1, 1st2) that takes two sorted lists as arguments, and returns the elements of both lists, merged. This function must have a complexity of O(n + m) where n list the length of 1st1 and m is the length of 1st2. Part B: Merge Sort Implement the iterative version of merge_sort (1st), where 1st is an unsorted list, you should also return the sorted list. Your code should call merge (1st1, 1st2) from Part A. Part C: Analysis of Sorting Algorithms Ensure your Merge Sort, Insertion Sort and Selection Sort functions from this week and last week are fully functional before commencing this task. Over the past couple of weeks you have been introduced to several different sorting algorithms. We will analyse the run times of these algorithms and compare them against each other to recognise the different computational complexities they present. You should first: Copy and paste your insertion sort and selection sort functions from last week into this week's workshop. Your task is to implement a function sort_analysis that takes two arguments, func, a function as an pa- rameter representing a sorting algorithm and n, representing the number of elements in a list to be sorted. sort_analysis should return a dictionary of run times with string representations of 'non-decreasing', 'decreas- ing' and 'random' as keys and their respective run times as values. For Example: >>> from workshop07 import merge_sort, merge >>> sort_analysis (merge-sort, 1000) # Merge Sort with 1000 elements {'non-decreasing': 0.0025189999999923884, 'decreasing': 0.002601599999991322, 'random': 0.003887900000009381} Note: Your values do not have to match the example values as they are dependent on your computer's clock. Inside sort_analysis you are required to: Construct three lists to test your sorting algorithms. You should have one list with non-decreasing, one for decreasing and one for randomised numbers ranging from 1 to n inclusively. . Consider how you would randomise your random list and ensure you are using the same random list across all of your algorithms. Have a look into Python's random module about shuffling and creating a seed. https://docs.python.org/3/library/random.html. Extract the system timer to get the run times of the algorithm, Hint: There are a couple of options to implement this, but one way is to extract the start and end times of a function. Have a look at Python's timeit and/or time modules to figure this out. https://docs.python.org/3.8/library/timeit.html. Once complete, test the performance of each sorting algorithms against each other by using the sort_analysis function in the Python shell and manipulating the input size. Then write a paragraph inside the docstring about any of the observations you have made. Can you determine the time-complexities of each sorting algorithm given different lists (i.e., non-decreasing, decreasing, random). Task 2: Closing down a post office Assume you are an executive of a postal service company and, unfortunately, due to shrinking demand in delivering physical letters, you have close down one of your post offices. To reduce the spatial coverage of your letter distribution network as little as possible, you would like to merge two offices that are closest to each other. Here, you consider as distance the usual Euclidean distance. That is, if (x1, yl) are the coordinates of one post office and (x2, y2) are the coordinates of another, then their distance is given as: dist ((x1, y), (x2, y2)) = (x1 x2) + (y1 Y2) . (1) The partial specification of a function offices_to_merge (points) that you can use to determine which post offices to merge is as follows: Input: list of 2d coordinates of post offices: points=[(x1, y1), (x2, y2)... (xn, yn)] with n>1. Output: a pair of indices i, j such that. For example: = >>> points [(350, 150), (500, 250), (150, 150), (50, 400), (200,100)] >>> offices_to_merge (points) (2, 4) 1. Write a Python function with a completed output specification in the documentation. 2. Implement and algorithm that correctly solves the problem given in the specification. 3. Make sure that your algorithm does not compute the distances between identical points more than once. Explain how you have achieved this in a separate paragraph in your function documentation. 4. What is the computational complexity of your algorithm? Give an analysis in another paragraph of your function documentation. 5. Optional: Annotate an invariant in your function that shows that the correct output is computed. Task 1: Merge Sort Part A: Merge Write a function merge (1st1, 1st2) that takes two sorted lists as arguments, and returns the elements of both lists, merged. This function must have a complexity of O(n + m) where n list the length of 1st1 and m is the length of 1st2. Part B: Merge Sort Implement the iterative version of merge_sort (1st), where 1st is an unsorted list, you should also return the sorted list. Your code should call merge (1st1, 1st2) from Part A. Part C: Analysis of Sorting Algorithms Ensure your Merge Sort, Insertion Sort and Selection Sort functions from this week and last week are fully functional before commencing this task. Over the past couple of weeks you have been introduced to several different sorting algorithms. We will analyse the run times of these algorithms and compare them against each other to recognise the different computational complexities they present. You should first: Copy and paste your insertion sort and selection sort functions from last week into this week's workshop. Your task is to implement a function sort_analysis that takes two arguments, func, a function as an pa- rameter representing a sorting algorithm and n, representing the number of elements in a list to be sorted. sort_analysis should return a dictionary of run times with string representations of 'non-decreasing', 'decreas- ing' and 'random' as keys and their respective run times as values. For Example: >>> from workshop07 import merge_sort, merge >>> sort_analysis (merge-sort, 1000) # Merge Sort with 1000 elements {'non-decreasing': 0.0025189999999923884, 'decreasing': 0.002601599999991322, 'random': 0.003887900000009381} Note: Your values do not have to match the example values as they are dependent on your computer's clock. Inside sort_analysis you are required to: Construct three lists to test your sorting algorithms. You should have one list with non-decreasing, one for decreasing and one for randomised numbers ranging from 1 to n inclusively. . Consider how you would randomise your random list and ensure you are using the same random list across all of your algorithms. Have a look into Python's random module about shuffling and creating a seed. https://docs.python.org/3/library/random.html. Extract the system timer to get the run times of the algorithm, Hint: There are a couple of options to implement this, but one way is to extract the start and end times of a function. Have a look at Python's timeit and/or time modules to figure this out. https://docs.python.org/3.8/library/timeit.html. Once complete, test the performance of each sorting algorithms against each other by using the sort_analysis function in the Python shell and manipulating the input size. Then write a paragraph inside the docstring about any of the observations you have made. Can you determine the time-complexities of each sorting algorithm given different lists (i.e., non-decreasing, decreasing, random). Task 2: Closing down a post office Assume you are an executive of a postal service company and, unfortunately, due to shrinking demand in delivering physical letters, you have close down one of your post offices. To reduce the spatial coverage of your letter distribution network as little as possible, you would like to merge two offices that are closest to each other. Here, you consider as distance the usual Euclidean distance. That is, if (x1, yl) are the coordinates of one post office and (x2, y2) are the coordinates of another, then their distance is given as: dist ((x1, y), (x2, y2)) = (x1 x2) + (y1 Y2) . (1) The partial specification of a function offices_to_merge (points) that you can use to determine which post offices to merge is as follows: Input: list of 2d coordinates of post offices: points=[(x1, y1), (x2, y2)... (xn, yn)] with n>1. Output: a pair of indices i, j such that. For example: = >>> points [(350, 150), (500, 250), (150, 150), (50, 400), (200,100)] >>> offices_to_merge (points) (2, 4) 1. Write a Python function with a completed output specification in the documentation. 2. Implement and algorithm that correctly solves the problem given in the specification. 3. Make sure that your algorithm does not compute the distances between identical points more than once. Explain how you have achieved this in a separate paragraph in your function documentation. 4. What is the computational complexity of your algorithm? Give an analysis in another paragraph of your function documentation. 5. Optional: Annotate an invariant in your function that shows that the correct output is computed.
Expert Answer:
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
Here are definitions of sequences and trees with integer elements: type iseq = Nil | Cons of int * (unit -> iseq) type itree = Leaf of int | Branch of itree * itree (a) In an ascending sequence such...
-
Q1. You have identified a market opportunity for home media players that would cater for older members of the population. Many older people have difficulty in understanding the operating principles...
-
A strip of metal is originally 1.5 m long. It is stretched in three steps: first to a length of 1.75 m, then to 2.0 m, and finally to 3.0 m. Show that the total true strain is the sum of the true...
-
On December 31, after adjustments, The Dexter Family Farm's ledger contains the following account balances. 101...
-
A small immersion heater is connected to a power supply of e.m.f. of 12 V for a time of 150 s. The output power of the heater is 100 W. What charge passes through the heater? A. 1.4 C B. 8.0 C C....
-
A pure sine wave has a maximum peak value of 1.2 V. What is the area under the curve?
-
1. Are there any disagreements between or among the resulting orderings? If so, why do you think that is the case? 2. What can be done to mitigate any observed disagreement between or among the...
-
Please use PESTEL to analyze the current business environment. In other words, what are some political/legal, economic, sociocultural, technological, and ecological trends now that are impactful for...
-
Discuss any security situations that you have personally encountered and how they were handled by the business organization. Do you feel comfortable with how businesses are safekeeping your data and...
-
Suppose events A, B, and C are independent and \[P(A)=\frac{1}{2} \quad P(B)=\frac{1}{3} \quad P(C)=\frac{1}{6}\] Find the probabilities in Problems 5-12. a. \(P(\bar{A})\) b. \(P(\overline{A \cap...
-
Consider a state lottery that has a weekly television show. On this show, a contestant receives the opportunity to win \(\$ 1\) million. The contestant picks from four hidden windows. Behind each is...
-
Determine whether each of the figures in Problems 30-37 will be a solution to an Instant Insanity puzzle.
-
Use estimation to select the best response in Problems 7-12. Do not calculate. If the expected value of playing a \(\$ 1\) game of blackjack is \(\$ 0.04\), then after playing the game 100 times you...
-
Consider the spinners in Problems 31-34. Determine which represent fair games. Assume that the cost to spin the wheel once is \(\$ 5.00\) and that you will receive the amount shown on the spinner...
-
Stephanie Adkins is an accountant who is trained in forensic accounting. She is an experienced fraud investigator. She was recently hired by Lake Side Hotels, a closely-held corporation, to...
-
Carlton Stokes owns and operates a car-detailing business named SuperShine & Detailing. For $150, Carltons business will hand wash and wax customers cars, vacuum the interior, and thoroughly clean...
-
We say that a binary search tree T 1 can be right-converted to binary search tree T 2 if it is possible to obtain T 2 from T 1 via a series of calls to RIGHT-ROTATE. Give an example of two trees T 1...
-
Let G = (V, E) be a directed graph with weight function w : E R, and let n = |V|. We define the mean weight of a cycle c = e 1 , e 2 , . . . , e k of edges in E to be Let * = min c (c), where c...
-
When RANDOMIZED-QUICKSORT runs, how many calls are made to the random number generator RANDOM in the worst case? How about in the best case? Give your answer in terms of -notation.
-
With reference to Exercise 10.55, find a large sample 95% confidence interval for the true difference of probabilities. Data From Exercise 10.55 10.55 As a check on the quality of eye glasses...
-
As a check on the quality of eye glasses purchased over the internet, glasses were individually ordered from several different online vendors. Among the 92 lenses with antireflection coating, 61...
-
Two bonding agents, \(A\) and \(B\), are available for making a laminated beam. Of 50 beams made with Agent \(A, 11\) failed a stress test, whereas 19 of the 50 beams made with Agent \(B\) failed. At...
Study smarter with the SolutionInn App