Question

In: Computer Science

7-1 Hoare partition correctness The version of PARTITION given in this chapter is not the original...

7-1 Hoare partition correctness
The version of PARTITION given in this chapter is not the original partitioning
algorithm. Here is the original partition algorithm, which is due to C. A. R. Hoare:
HOARE-PARTITION.A; p; r/
1 x D AOEp
2 i D p 1
3 j D r C 1
4 while TRUE
5 repeat
6 j D j 1
7 until AOEj x
8 repeat
9 i D i C 1
10 until AOEi x
11 if i < j
12 exchange AOEi with AOEj
13 else return j
a. Demonstrate the operation of HOARE-PARTITION on the array A D h13; 19; 9;
5; 12; 8; 7; 4; 11; 2; 6; 21i, showing the values of the array and auxiliary values
after each iteration of the while loop in lines 4–13.

The next three questions ask you to give a careful argument that the procedure
HOARE-PARTITION is correct. Assuming that the subarray AOEp : : r contains at
least two elements, prove the following:
b. The indices i and j are such that we never access an element of A outside the
subarray AOEp : : r.
c. When HOARE-PARTITION terminates, it returns a value j such that p j < r.
d. Every element of AOEp : : j is less than or equal to every element of AOEj C1: : r
when HOARE-PARTITION terminates.
The PARTITION procedure in Section 7.1 separates the pivot value (originally
in AOEr) from the two partitions it forms. The HOARE-PARTITION procedure, on
the other hand, always places the pivot value (originally in AOEp) into one of the
two partitions AOEp : : j and AOEj C 1: : r. Since p j < r, this split is always
nontrivial.
e. Rewrite the QUICKSORT procedure to use HOARE-PARTITION.

Solutions

Expert Solution

a)

b)

In the first loop, i will terminate at the pivot, the smallest j would be the pivot, therefore no invalid position is accessed. In the next loops, i will finally terminate at last j and i will finally terminate at last i, and since and after the first loop, there is no change to access an element outside A[p…r].

c)

In b, we know the largest j in the first loop is r, while i will be at p, if then the loop will not terminate. In the second loop, jj has to move at least one step, therefore j must be less than r.

e)

def hoare_partition(a, p, r):
    x = a[p]
    i = p - 1
    j = r
    while True:
        while True:
            j -= 1
            if a[j] <= x:
                break
        while True:
            i += 1
            if a[i] >= x:
                break
        if i < j:
            a[i], a[j] = a[j], a[i]
        else:
            return j


def quicksort(a, p, r):
    if p < r - 1:
        q = hoare_partition(a, p, r)
        quicksort(a, p, q + 1)
        quicksort(a, q + 1, r)

Related Solutions

Prove it's correct or wrong. A particular version of PARTITION runs in logarithmic time but produces...
Prove it's correct or wrong. A particular version of PARTITION runs in logarithmic time but produces splits where all elements go to the same side of the pivot every time. QUICKSORT based on this PARTITION has the same worse-case running time as the standard version.
Item 7 In the case below, the original source material is given along with a sample...
Item 7 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version There is a design methodology called rapid prototyping, which has been used successfully in software engineering. Given similarities between software design and instructional design, we argue that rapid prototyping is a viable method for instructional design, especially for computer-based instruction. References: Tripp, S. D., &...
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)...
. (This is a version of Programming Project 2.1 from Chapter 2.) The Babylonian algorithm to...
. (This is a version of Programming Project 2.1 from Chapter 2.) The Babylonian algorithm to compute the square root of a positive number n is as follows: 1. Make a guess at the answer (you can pick n/2 as your initial guess). 2. Compute r = n / guess. 3. Set guess = (guess +r) / 2. 4. Go back to step 2 until the last two guess values are within 1% of each other. Write a JAVA program...
In Chapter 7, we saw that if the market interest rate, rd, for a given bond increased
In Chapter 7, we saw that if the market interest rate, rd, for a given bond increased, the price of the bond would decline. Applying this same logic to stocks, explain(a) how a decrease in risk aversion would affect stocks’ prices and earned rates of return,(b) how this would affect risk premiums as measured by the historical difference between returns on stocks and returns on bonds, and(c) what the implications of this would be for the use of historical risk...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
Item 1 In the case below, the original source material is given along with a sample...
Item 1 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version There is a desperate need for theorists and researchers to generate and refine a new breed of learning-focused instructional design theories that help educators and trainers to meet those needs, (i.e., that focus on learning and that foster development of initiative, teamwork, thinking skills, and...
Item 1 In the case below, the original source material is given along with a sample...
Item 1 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version Obviously, it is vitally important in the war of attrition that individuals should give no inkling of when they are going to give up. Anybody who betrayed, by the merest flicker of a whisker, that he was beginning to think of throwing in the sponge,...
Item 1 In the case below, the original source material is given along with a sample...
Item 1 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version Whereas Gauguin was an iconoclast, caustic in speech, cynical, indifferent, and at times brutal to others, Vincent van Gogh (1853-90) was filled with a spirit of enthusiasm for his fellow artists and overwhelming love for humanity. References: Arnason, H. H. (2003). History of modern art:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT