Question

In: Computer Science

4-1- Let array locations A[0], ..., A[7] hold numbers A = {9,8,7,6,5,4,3,2}. Carry out by hand...

4-1- Let array locations A[0], ..., A[7] hold numbers A = {9,8,7,6,5,4,3,2}. Carry out by hand one call of the partition function of Quick Sort on the array A above. You must trace the actions of partition on this array carefully. After this single call to partition, answer the questions:

1. What is the initial index of the pivot element? What is its value?

2. Where does the pivot end up? (What is its index at the end?)

3. How many non-trivial interchanges are there? (Interchanges of two distinct elements.)

4. How many empty interchanges are there? (Interchanges of an element with itself.)

5. What value is returned by this call to partition?

6. What is the result after this single call to partition? Identify the left sub-array of the partition and the right sub-array of the partition.

7. Suppose we continue with the quicksort algorithm beyond this initial call to partition. Briefly describe with this specific example how the rest of the sorting goes, until the array is sorted

Solutions

Expert Solution

1. What is the initial index of the pivot element? What is its value?

answer: [Init pivot index: 7, Init pivot value: 2 ]

2. Where does the pivot end up? (What is its index at the end?)

answer:   [Pivot ends up in index 0, at the far left; there's nowhere else it could be.]

3. How many non-trivial interchanges are there? (Interchanges of two distinct elements.)

answer: [During execution, i is never incremented inside the loop, so there are no interchanges inside the loop.

At t he end there is a single non-trivial interchange of A[0] with A[7], putting 2 at the beginning

and 9 at the end.]

4. How many empty interchanges are there? (Interchanges of an element with itself.)

answer: [ 0, see above ]

5. What value is returned by this call to partition?

answer: [ returns the index of the final location of the pivot, namely 0 ]

6. What is the result after this single call to partition? Identify the left sub-array of the partition and the right  sub-array of the partition.

answer: [ left sub-array empty, pivot is in location A[0], and right sub-array is A[1] to A[7]: {8,7,6,5,4,3,9} ]

7. Suppose we continue with the quicksort algorithm beyond this initial call to partition.

Briefly describe with this specific example how the rest of the sorting goes, until the array

is sorted.

answer: [ Below, the underlined numbers are input to the next call to partition
{9,8,7,6,5,4,3,2} (1st call, moves 2 to beginning, 1 non-empty interchange)
   {2,8,7,6,5,4,3,9} (2nd call, drops 9 from consideration, empty interchanges)
{2,8,7,6,5,4,3,9} (3rd call, moves 3 to left, 1 non-empty interchange)
{2,3,7,6,5,4,8,9} (4th call, drops 8 from consideration, empty interchanges)
{2,3,7,6,5,4,8,9} (5th call, moves 4 to left, 1 non-empty interchange)
{2,3,4,6,5,7,8,9} (6th call, drops 7 from consideration, empty interchanges)
{2,3,4,5,6,7,8,9} (7th call, moves 5 to left, 1 non-empty interchange) ]


Related Solutions

Write an array method to carry out each of the following tasks for an array of...
Write an array method to carry out each of the following tasks for an array of integers. Note: you can think of each as a separate problem independent of the others. a) Swap the first and last elements in the array. b) Shi< all elements by one to the right and move the last element into the first posi>on. For example, 1 4 9 16 25 would be transformed into 25 1 4 9 16. c) Replace all even elements...
Write array methods that carry out the following tasks for an array of integers by creating...
Write array methods that carry out the following tasks for an array of integers by creating and completing the “ArrayMethods” class below. Add documentation comments for each method. Provide a test program called ‘Lab5_yourID.java” that test methods of ArrayMethods class. In your test program, use random class to generate array values. public class ArrayMethods { private int[ ] values; //declare instant variables public ArrayMethods (int[ ] initialValues) {values = initialValues;} //constructor public void shiftRight( ) { … } public Boolean...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a...
algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a number output result: true if x in a false otherwise Sideeffect NA Plan if (top < bottom) result := false else // get the middle of the array (refining) middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …) // example what is midpoint between 6 and 8 // (8-6)/2 = 1 we need to add 6 to...
4) Let ? = {2, 3, 5, 7}, ? = {3, 5, 7}, ? = {1,...
4) Let ? = {2, 3, 5, 7}, ? = {3, 5, 7}, ? = {1, 7}. Answer the following questions, giving reasons for your answers. a) Is ? ⊆ ?? b)Is ? ⊆ ?? c) Is ? ⊂ ?? d) Is ? ⊆ ?? e) Is ? ⊆ ?? 5) Let ? = {1, 3, 4} and ? = {2, 3, 6}. Use set-roster notation to write each of the following sets, and indicate the number of elements in...
Fill in the blanks of this code to print out the numbers 1 through 7. number...
Fill in the blanks of this code to print out the numbers 1 through 7. number = 1 while number ___ 7:     print(number, end=" ")     ___
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1...
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1 0 2 11 5 11 27 4 0 6 Create Standard Deviation Chart (Normal Distribution Curve)
4. The check digit for ISBNs is one of the numbers 0, 1, 2, . ....
4. The check digit for ISBNs is one of the numbers 0, 1, 2, . . . , 9, or the letter X. One of the your fellow students comments ”Gee, it sure is a pain to have to use that X all time. Why don’t they just compute the check digit sum modulo 10 instead of modulo 11, so that we can get rid of the X?” Would this plan work? Prove your answer. Assume that all ISBNs are...
Carry out a one-way ANOVA by hand to test the following research question: Carl wants to...
Carry out a one-way ANOVA by hand to test the following research question: Carl wants to know if drinking beer (2, 5 or 8 beers) affects the number of times people sing karaoke  at a local bar in a given night. He is predicting that the groups will differ significantly (α = .05). (Perform post-hoc tests if necessary) 2 beers 5 beers 8 beers 1 2 4 2 1 3 1 3 5 3 2 2
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT