3. The following algorithm sorts the numbers in the array from smallest to largest. How can...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
3. The following algorithm sorts the numbers in the array from smallest to largest. How can you modify it so the sorting is now done from largest to smallest? Please, do your own modification first (even better, code it!) and then, look at the possible answers. A: array of numbers N: number of elements in array A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 function Sort (A, N) swapped=true while (swapped) swapped=false for 0 < i < N-1 if (A[i]> A[i+1]) then end end for N=N-1 end while return A end function swap(A[i], A[i+1]) swapped=true if none of the others the instruction swap (A[i], A[i+1]) must be changed to swap(A[i+1], A[i]) the instruction swapped=true must be changed to swapped-false and vice versa the specification of the for loop in line 5 must be changed to: for N-1<i<= 0 do the condition A[i] > A[i+1] in line 6 must be changed to A[i] <A[i+1] 3. The following algorithm sorts the numbers in the array from smallest to largest. How can you modify it so the sorting is now done from largest to smallest? Please, do your own modification first (even better, code it!) and then, look at the possible answers. A: array of numbers N: number of elements in array A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 function Sort (A, N) swapped=true while (swapped) swapped=false for 0 < i < N-1 if (A[i]> A[i+1]) then end end for N=N-1 end while return A end function swap(A[i], A[i+1]) swapped=true if none of the others the instruction swap (A[i], A[i+1]) must be changed to swap(A[i+1], A[i]) the instruction swapped=true must be changed to swapped-false and vice versa the specification of the for loop in line 5 must be changed to: for N-1<i<= 0 do the condition A[i] > A[i+1] in line 6 must be changed to A[i] <A[i+1]
Expert Answer:
Answer rating: 100% (QA)
The given code appears to be implementing a bubble sort algorithm to sort an array A of N numbers ... View the full 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
-
What is the spring cloud API Gateway and how do you set it up to make a call to the product service via eureka service discovery server? Give me the complete code in detailed steps.
-
Design a controller to stabilize the system G(S) = with feedback H(S) +371
-
Assume a retailer has fixed costs of $10,000, a unitvariable cost of $25, and a 50% retail margin. How many units must be sold for her tobreak-even? If she has a target profit of $200,000, how many...
-
Prepare a strategic marketing plan for a company that is considering entering the Latin American market.
-
Describe, compare, and contrast the concepts of future value and present value. Explain the role of the discount rate in calculating present value.
-
A salespersons past and present performances affect his or her expectations for future performance. After experiencing several failures, many new salespeople quit their sales job within a few months...
-
The balance sheet of Kishwaukee Corporation as of December 31, 2014, is as follows. Note 1: Buildings are stated at cost, except for one building that was recorded at appraised value. The excess of...
-
Arthur has a capital balance of $80,000 and E . Joseph has a capital balance of $100,000 in their partnership as of June 30 . On July 1 , the two partners agree to accept M. Alice as a partner in...
-
Jerry Stevenson is the manager of a medical clinic in Scottsdale, AZ. He wants to analyze patient data to identify high-risk patients for cardiovascular diseases. From medical literature, he learned...
-
Illustrate the process of normalizing the data shown in the form below Illustrate the process of normalizing the data shown in the form below 18-Oct-12 DreamHome Property Inspection Report...
-
Sink Corporation prepared the following summarized balance sheet on January 1, 20X1: Plumber Company acquires 80 percent of Sink Corporations common stock on January 1, 20X1, for $80,000. At that...
-
A laptop battery has an emf of 10.8 V. The laptop uses 0.70 A while running. a. How much charge moves through the battery each second? b. By how much does the electric potential energy of this charge...
-
A battery and a resistor are wired into a circuit. The resistor dissipates 0.50 W. Now two batteries, each identical to the original one, are connected in series with the resistor. What power does it...
-
A battery and a resistor are wired into a circuit. The resistor dissipates 4.0 W. Now the resistor is replaced with one that has twice the original resistance. What power does the new resistor...
-
A 100 W lightbulb and a 60 W lightbulb each operate at a voltage of 120 V. Which bulb carries more current?
-
ask you to compare and contrast Thomas Hobbes conception of the state of nature with the perspective on government and human nature that is offered by Derrick Jensen as expressed in the film entitled...
-
Decades after the event, Johnson & Johnson (J&J), the 130-year-old American multinational, is still praised for swiftly The company indicated that its response was based on the expectations set forth...
-
Show the results of deleting C, P, and V , in order, from the tree of Figure 18.8(f). Figure 18.8
-
Given two points p 1 and p 2 in the plane, the L -distance between them is given by max(|x 1 x 2 | , |y 1 y 2 |). Modify the closest-pair algorithm to use the L -distance.
-
Show that b (k ; n, p) = b (n k ; n, q), where q = 1 p.
-
Equity balances for Sen Widyaya appearing in the statement of financial positions of Widyayas Window Washing Services as at 30 June 2024, 2023 and 2022 are set out below. During 20222023, Sen...
-
A friend who has established a new Pilates studio, Core Strength, has asked you to give some advice as to the contents of financial statements. Transactions of Core Strength include: (a) contribution...
-
Month-end statement of financial position amounts for the practice of David Wei, an immigration lawyer, for 3 consecutive years are shown below. The information is complete except for the balance in...
Study smarter with the SolutionInn App