A key procedure in the Quicksort algorithm is to recursively partition the array elements about a pivot
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
1 2 3 4 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 //quicksort the subarray from a[lo] to a[hi] private static void quicksort (Comparable [] a, int lo, int hi) { if (hi <= lo) return; int j = partition (a, lo, hi); quicksort (a, lo, j-1); quicksort (a, j+1, hi); } // partition the subarray a[lo hi] by returning an indez j // so that allo j-1] <= a[j] <= a[j+1 .. hi] private static int partition (Comparable [] a, int lo, int hi) { int i = lo; int j = hi + 1; Comparable v = a [lo]; while (true) { } //select the pivot // find item on lo to swap while (less (a[++i], v)) = hi) break; // find item on hi to swap while (less (v, a[j])) = = lo) break; // check if pointers croSS if (ij) break; exch(a, i, j); } // put v = alj into position exch(a, lo, j); // with allo j-1] <= a[j] <= a[j+1. hi] return j 1 2 3 4 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 //quicksort the subarray from a[lo] to a[hi] private static void quicksort (Comparable [] a, int lo, int hi) { if (hi <= lo) return; int j = partition (a, lo, hi); quicksort (a, lo, j-1); quicksort (a, j+1, hi); } // partition the subarray a[lo hi] by returning an indez j // so that allo j-1] <= a[j] <= a[j+1 .. hi] private static int partition (Comparable [] a, int lo, int hi) { int i = lo; int j = hi + 1; Comparable v = a [lo]; while (true) { } //select the pivot // find item on lo to swap while (less (a[++i], v)) = hi) break; // find item on hi to swap while (less (v, a[j])) = = lo) break; // check if pointers croSS if (ij) break; exch(a, i, j); } // put v = alj into position exch(a, lo, j); // with allo j-1] <= a[j] <= a[j+1. hi] return j
Expert Answer:
Answer rating: 100% (QA)
array 22 34 32 60 55 7 63 10 15 25 Step 1 Choosing the pivot 22 We select the first element 22 as the pivot Pivot 22 The array before partition 22 34 ... 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
-
A manufacturer purchases a part for use at both of its plants-one at Roseville, California, the other at Akron, Ohio. The part is available in limited quantities from two suppliers. Each supplier has...
-
If the focal length of a lens is 3 centimeters and the image distance is 5 centimeters from the lens, what is the distance from the object to the lens?
-
(a) Let Find vectors -12 -5] -- -- [18-[1] 1 = = S = -5 to U = A = U = in R such that S is the transition matrix from (v1, v2} to {u, u}- (b) Let P4 be the vectors space of all polynomials of degree...
-
What personal constructs do you rely on when perceiving the people in this photo?
-
An AM radio station radiates an isotropic sinusoidal wave with an average power of 50 kW. What are the amplitudes of Emax and Bmax at a distance of (a) 500 m, (b) 5 km, and (c) 50 km?
-
Why is it that an American option is always worth the same as a European option? If it were not, what strategy could an arbitrageur use to profit?
-
What is the purpose of practice aids in forensic and valuation services?
-
Fultz Companys organization chart includes the president; the vice president of production; three assembly plantsDallas, Atlanta, and Tucson; and two departments within each plantMachining and...
-
How can a healthcare business increase its productivity and profitability using the disciplines found in Six Sigma?
-
Two power plants are currently emitting 8,000 tons of pollution each (for a total of 16,000 tons) in City A. Marginal pollution reduction costs for Plant 1 are given by MCR 1 = 0.02 Q 1 and for Plant...
-
Atkinson Stainless Steel Fabrication Company (ASSFC) are specialist in designing and fabricating the stainless steel tanks for pharmaceutical, Food and Chemical industries. Kaza Pharma Inc. (KPI)...
-
With dealing with intellectual property, there are two ways it can be enforced, being common law and statutory law. For example with patents, what would be the "punishment" or enforcement I should...
-
Why do problems at work stay undetected for so long? Does anyone like to be wrong? How do you react when someone informs you of a mistake? Can you think of any specific examples and reactions that...
-
Suppose you run a business making pizzas. Over a year, you pay $18,000 for the ingredients, equipment, etc. that you need to make the pizzas, and your total revenue from selling pizza is $60,000. Had...
-
How do mutualistic interactions contribute to the resilience of agricultural systems, and what strategies can be employed to enhance the sustainability and productivity of agroecosystems through the...
-
Ken Barker received a $10,000 scholarship to study undergraduate engineering at Rainbow University for the current academic year. Ken received the $10,000 in cash, and he used the money as follows:...
-
4. (20 points) Given that there are 2 inferior products out of 10, and 2 pieces are taken without replacement ("without replacement" means that each piece is NOT put back before the next piece is...
-
Explain the operation of the dividends received deduction.
-
Make a 3-by-3 chart with row and column labels WHITE, GRAY, and BLACK. In each cell (I, j), indicate whether, at any point during a depth-first search of a directed graph, there can be an edge from a...
-
GREEDY-SET-COVER can return a number of different solutions, depending on how we break ties in line 4. Give a procedure BAD-SET-COVER-INSTANCE (n) that returns an n-element instance of the...
-
Suppose that we are given a weighted, directed graph G = (V, E) in which edges that leave the source vertex s may have negative weights, all other edge weights are nonnegative, and there are no...
-
N = 230, n = 15, k = 200 Compute the mean and standard deviation of the hypergeometric random variable X.
-
One study showed that in a certain year, airline fatalities occur at the rate of 0.011 deaths per 100 million miles. Find the probability that, during the next 100 million miles of flight, there will...
-
N = 60, n = 8, k = 25 Compute the mean and standard deviation of the hypergeometric random variable X.
Study smarter with the SolutionInn App