In this problem you will implement a function to sort a list of integers or floats...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
In this problem you will implement a function to sort a list of integers or floats in non-decreasing order using the selection sort algorithm. The idea of Selection of Sort is as follows. To sort a length-n list L = [L[0],..., L[n 1]]: 1. Find the index minIndex of smallest element in L (find the index of first occurrence if you have more than one element equal to the min). 2. Exchange L[minIndex] (swap it) with L[0], hence now L[0] is the (a) smallest element of L, i.e., the number stored in L[0] is in its correct position in the desired sorted order 3. Find the index minIndex of the smallest element in [L[1], ..., L[n 1]] 4. Exchange L[min Index] with L[1], hence now L[1] is the (a) second smallest element of L, i.e., the number stored in L[1] is in its correct position in the desired sorted order. 5. Find the index minIndex of the smallest element in [L[2], ..., L[n 1]] 6. Exchange L[minIndex] with L[2], hence now L[2] is the (a) third smallest element of L, i.e., the number stored in L[2] is in its correct position in the desired sorted order. 7. And so on until the whole list L is sorted You will implement this algorithm in Part (c), but first you will try it on an example in part (a) and you will implement a function to do Steps 1,3,5,... in Part (b). a) Try it on an example. Illustrate the algorithm on L= [5, 2, 4, 6, 1, 3]. There is no need to submit this part. b) Function to find the index of min. Implement a function indexOfMin (L, start, end), which takes as input arguments a list L of integers or floats and two indices start and end such that 0 < start < end Test script: L = [10,4,3,7,3,15] print (indexOfMin (L, 0,5)) print (indexOfMin (L,1,5)) print (indexOfMin (L,3,4)) print (indexOfMin (L, 4,4)) Output: NE44 2 2 c) Selection sort function. Using your function in Part (b), implement a function selectionSort (L), which given a list L of integers floats, sorts L in non-decreasing order as explained in the selection sort algorithm. Note that unlike the function indexOfMin, the function slectionSort modifies the list L. Notes: * Note that the slicing operator is not useful in the implementing of selectionSort. * Swapping: Say that we want to swap, i.e., exchange, L[7] and L[2]. If you set L[7] to L[2], and then set L[2] to L[7], you loose the old value of L[7]. To resolve this issue, use a temporary variable temp: 1. set a temp to L[7] 2. set L[7] to L[2], then 3. set L[2] to temp. Another approach to swap is use tuples: (L[7], L[2]) = (L[2], L[7]). In this problem you will implement a function to sort a list of integers or floats in non-decreasing order using the selection sort algorithm. The idea of Selection of Sort is as follows. To sort a length-n list L = [L[0],..., L[n 1]]: 1. Find the index minIndex of smallest element in L (find the index of first occurrence if you have more than one element equal to the min). 2. Exchange L[minIndex] (swap it) with L[0], hence now L[0] is the (a) smallest element of L, i.e., the number stored in L[0] is in its correct position in the desired sorted order 3. Find the index minIndex of the smallest element in [L[1], ..., L[n 1]] 4. Exchange L[min Index] with L[1], hence now L[1] is the (a) second smallest element of L, i.e., the number stored in L[1] is in its correct position in the desired sorted order. 5. Find the index minIndex of the smallest element in [L[2], ..., L[n 1]] 6. Exchange L[minIndex] with L[2], hence now L[2] is the (a) third smallest element of L, i.e., the number stored in L[2] is in its correct position in the desired sorted order. 7. And so on until the whole list L is sorted You will implement this algorithm in Part (c), but first you will try it on an example in part (a) and you will implement a function to do Steps 1,3,5,... in Part (b). a) Try it on an example. Illustrate the algorithm on L= [5, 2, 4, 6, 1, 3]. There is no need to submit this part. b) Function to find the index of min. Implement a function indexOfMin (L, start, end), which takes as input arguments a list L of integers or floats and two indices start and end such that 0 < start < end Test script: L = [10,4,3,7,3,15] print (indexOfMin (L, 0,5)) print (indexOfMin (L,1,5)) print (indexOfMin (L,3,4)) print (indexOfMin (L, 4,4)) Output: NE44 2 2 c) Selection sort function. Using your function in Part (b), implement a function selectionSort (L), which given a list L of integers floats, sorts L in non-decreasing order as explained in the selection sort algorithm. Note that unlike the function indexOfMin, the function slectionSort modifies the list L. Notes: * Note that the slicing operator is not useful in the implementing of selectionSort. * Swapping: Say that we want to swap, i.e., exchange, L[7] and L[2]. If you set L[7] to L[2], and then set L[2] to L[7], you loose the old value of L[7]. To resolve this issue, use a temporary variable temp: 1. set a temp to L[7] 2. set L[7] to L[2], then 3. set L[2] to temp. Another approach to swap is use tuples: (L[7], L[2]) = (L[2], L[7]).
Expert Answer:
Answer rating: 100% (QA)
Answer def indexOfMinL start end if start 0 or end lenL or start end I... View the full answer
Related Book For
Discrete Mathematics and Its Applications
ISBN: 978-0073383095
7th edition
Authors: Kenneth H. Rosen
Posted Date:
Students also viewed these programming questions
-
Write a program that reads a number nuggets , (input by the user), and then calculates how many calories and displays the result. Use a named constant CALORIES_PER_NUGGET . The value to use is: 59.3...
-
Assignment 5: Hash Table implementation andconcordance There are three parts to this assignment. In the first two parts,you will complete the implementation of a hash map and aconcordance program. In...
-
How does the integration of positive psychology principles, such as strengths-based approaches and flow theory, contribute to the enhancement of motivation and well-being in individuals and...
-
Mr. and Mrs. Thomas are buying a new house for $330,000. Compare the two loan offers by finding the monthly payments for each and calculating the total amount paid for each. Loan Options Down Payment...
-
Why do you seem to lurch forward in a bus that suddenly slows? Why do you seem to lurch backward when the bus picks up speed? What law applies here?
-
Les Cliniques du Parc reports the following information for July 2022 regarding its nursing staff consisting of nurses, nursing assistants and orderlies. Required 1 Calculate the total direct nursing...
-
Interest rate parity Consider Figure 3.29, which indicates (approximately) the difference between interest rates in the United States and the UK, Rus Ruk on a one-year T-bill (i.e. a two-year bill...
-
The management of Weimar Inc., a civil engineering design company, is considering an investment in a high-quality blueprint printer with the following cash flows: Required: 1. Determine the payback...
-
Illustrate and discuss how an autonomous increase in the expected rate of inflation will change the demand and supply of loanable funds and the equilibrium nominal interest rate. Discuss the effects...
-
You are part of the engagement team for the audit of Suzuki Manufacturing for the year ended December 31, 2019, and are responsible for auditing the acquisition cycle. Download the Excel file for the...
-
K-Roo Ltd. is looking to establish a subsidiary in New Zealand. The estimated NZD cash flows for the subsidiary for the next three years (after which the subsidiary will be disestablished) are: Yro...
-
The company is looking to invest $800,000 for two addition lines for production.Our current ROI targets are at 15%.These two lines would then produce annual profits of $65,000 for each line.Do you...
-
1 . How would you calculate the loss of coastal land due to natural causes into your accounting books? 2 . how would this affect the property value of the house & land? 3 . is there any law or...
-
The Pandeys were looking at buying a new house in Surrey. Their bank used the 32% rule decided that they can afford a maximum monthly mortgage payment of $2,500. How large of a mortgage would the...
-
Fixed costs for manufacturing widgets are $25,000. Determine the fixed cost per unit when the quantity produced is (a) 5,000 and (b) 75,000.
-
The Pandeys just purchased a home in Burnaby for $1,500,000. They made a down payment of 20% and took out a mortgage with Westminster Savings Credit Union for the balance amortized over 25 years at...
-
The spreadsheet template includes financial data on the (1) January 2020 treasury curve, (2) Target's income statement, (3) Target's balance sheet, (4) Target's recent equity returns and January 2020...
-
Calculate the Lagrange polynomial P 2 (x) for the values (1.00) = 1.0000, (1.02) = 0.9888, (1.04) = 0.9784 of the gamma function [(24) in App. A3.1] and from it approximations of (1.01) and (1.03).
-
What are the codes for a, e, i, k, o, p, and u if the coding scheme is represented by this tree? 0 0 0 0 0
-
Describe a graph model that represents traditional marriages between men and women. Does this graph have any special properties?
-
Let X be a random variable on a sample space S such that X(s) 0 for all s S. Show that p(X(s) a) E(X)/afor every positive real number a. This inequality is called Markov's inequality.
-
\(106+75+94\) Use properties of real numbers and mental math to calculate the expression.
-
\(4 \times 72 \times 5\) Use properties of real numbers and mental math to calculate the expression.
-
\(43+62+17\) Use properties of real numbers and mental math to calculate the expression.
Study smarter with the SolutionInn App