Question

In: Computer Science

Below is the pseudocode for Quicksort and Partition that we talked about in class. As usual...

  1. Below is the pseudocode for Quicksort and Partition that we talked about in class. As usual with recursive functions on arrays, we see the array indices s and e as arguments. Quicksort(A, s, e) sorts the part of the array between s and e inclusively. The initial call (that is, to sort the entire array) is Quicksort(A, 0, n − 1)

QuickSort(A, s, e)

  if s < e

p = Partition (A, s, e) // Partition the array and return the position of pivot after the partition

  QuickSort(A, s, p-1)    // Sort left side

  QuickSort (A, p+1, e)    // Sort right side

  end if

Partition(A, s, e)

pivot = A[s], i = s + 1, j = e; // Let the leftmost element be the pivot

while i<=j // Rearrange elements

       while i < e & A[i] < pivot,

              i = i + 1

       end while

       while j > s & A[j] >= pivot,

              j = j - 1

       end while

       if i >= j

              break

       end if            

       swap A[i] nd A[j]

end while

swap A[s] nd A[j]

return j; // Return the index of pivot after the partition

  1. Let A = {11, 7, 6, 48, 30, 12, 75}, and assume we call Quicksort(A, 0, 6). Show what happens during the first invocation of Partition. What is the value of p returned, and what are the two recursive calls made?

Note: Credit will not be given only for answers - show all your work:

steps you took to get your answer.

your answer.

  1. In the given Partition function, we let the first element A[s] be the pivot. How do you modify Partition(A, s, e) so that it chooses the pivot as the median of three elements randomly selected from the array? pseudocode to change Partition.

Solutions

Expert Solution

(a) During the first partition, array is splitting to to two sub arrays.It is shown in follwoing figure

Value returned during first partition is

here, array is divided in to two sub array based on pivot.First array is{ 6,7}and second array is {48,30,12,75}. Pivot 11 is placed in exact position in sorted array. First recursive call partitions the first array as shown above Similary second recursive call partitions the second array.

(b) Function partition uses a function named find_from_median(A,s,e) for finding pivot element from median of 3 elements in the array

Partition(A, s, e)

pivot = find_from_median(A,s,e), i = s + 1, j = e; // Let the leftmost element be the pivot

while i<=j // Rearrange elements

       while i < e & A[i] < pivot,

              i = i + 1

       end while

       while j > s & A[j] >= pivot,

              j = j - 1

       end while

       if i >= j

              break

       end if            

       swap A[i] nd A[j]

end while

swap A[s] nd A[j]

return j; // Return the index of pivot after the partition

find_from_median(A,s,e)

k1=rand()

while(!(k1>=s & k1<=e))

k1=rand()

end while

k2=rand()

while(!(k2>=s & k2<=e))

k2=rand()

end while

k3=rand()

while(!(k3>=s & k3<=e))

k3=rand()

end while  

if ((A[k1] < A[k2] && A[k2] < A[k3]) || (A[k3] < A[k2] && A[k2] < A[k1]))

            return A[k2]  

        else if ((A[2] < A[1] && A[1] < A[3]) || (A[3] < A[1] && A[1] < A[2]))

return A[k1]

        else

        return A[k3]

end if


Related Solutions

In class we talked about the effects of cigarette smoking on birthweight. We defined a model...
In class we talked about the effects of cigarette smoking on birthweight. We defined a model E ( O | C = c ) = (β0)^1 + ((β1)^1 x c) where O is birthweight in ounces and C is cigarettes smoked per day. Suppose instead we used the model E(L|P =p)=(β0)^2 +((β1)^2 x p) where L is birthweight in pounds and P is packs of cigarettes smoked per day (there are 20 cigarettes in a pack). What is the relationship...
We talked about a diversity of respiratory strategies used by animals in class. One of the...
We talked about a diversity of respiratory strategies used by animals in class. One of the main differences between fish & frogs compared to reptiles, and mammals was that fish and frogs use buccal pumps and reptiles and mammals use suction pumps. How do buccal and suction pumps differ (please refer to where volume and pressure is changed and how it moves the respiratory medium through the respiratory system)? Explain how frogs use buccal pumps and mammals use suction pumps...
In our class we talked about impacts of COVID on the Canadian economy. by referring to...
In our class we talked about impacts of COVID on the Canadian economy. by referring to the class discussion explain possible impacts of COVID on AD and SRAS and appropriate polices for dealing with the problem.  
We talked in class about how some textbooks tell us that the way to conduct a...
We talked in class about how some textbooks tell us that the way to conduct a cost benefit analysisassociated with a policy is to look atthe costs and benefits beforea policy was put in place and compare them to the cost and benefits after the policy is in place. Is this correct? If so, explain why. If not, explain why not. NOTE: I realize that you should say "with" or "without" the policy, NOT "before" and "after" but I don't...
In class we talked about a number of different welfare measures. a) Graphically identify twoalternative money...
In class we talked about a number of different welfare measures. a) Graphically identify twoalternative money measures of welfare for an imposed change in the level of an environmental service flow (S1 > S0). b) Discuss the pros and cons of the measures you demonstrated above c) Distinguish between the compensating and equivalent measures using both expenditure and indirect utility function approaches. Explain the difference between the two in words.
In class, we talked about the elements for an effective signal. Courting male peacock spider Describe...
In class, we talked about the elements for an effective signal. Courting male peacock spider Describe the all of the components of this display. What modalities of communication might be in use (e.g. visual, etc.)? Write and explain hypotheses for the following questions: a. What makes this signal conspicuous? b. What information might the male be trying to convey to potential mates? How would you test for this? c. What might ensure that these signals are reliable and honest? d....
We talked about Ethnocentric, Polycentric, Regiocentric and Geocentric approaches to management in class. Discuss these 4...
We talked about Ethnocentric, Polycentric, Regiocentric and Geocentric approaches to management in class. Discuss these 4 approaches as if you were Adidas executive hiring staff for a production facility in China. Which approach would you recommend and why?
Quicksort - Divider and Conquer Illustrate the operation of PARTITION on the array: A = {9,...
Quicksort - Divider and Conquer Illustrate the operation of PARTITION on the array: A = {9, 19, 13, 5, 12, 8, 7, 4, 21, 6, 11}. Let A[1] = 9 be the pivot value.
We have talked in class about second order differential equations. These equations often arise in applicationsof...
We have talked in class about second order differential equations. These equations often arise in applicationsof Newtons second law of motion. For example, supposeyis the displacement of a moving object with massm. Its reasonable to think of two types of time-independent forces acting on the object. One type - suchas gravity - depends only on positiony. The second type - such as atmospheric resistance or friction -may depend on position and velocityy′. (Forces that depend on velocity are called damping...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given list a in non-descending order. Precondition: 0 <= l and u < len(a)''' if l < u: mid = (l + u) // 2 three = [a[l], a[mid], a[u]] three.sort() if three[1] == a[l]: pivot_loc = l elif three[1] == a[u]: pivot_loc = u else: pivot_loc = mid a[u], a[pivot_loc] = a[pivot_loc], a[u] pivot = a[u] i = partition(a, l, u - 1, pivot)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT