Introduction to Algorithms
Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
Question
Book Icon
Chapter 7, Problem 1P

(a)

Program Plan Intro

To show that the values of array and auxiliary values after each iterations of while loop using HOARE-PARTITION.

(a)

Expert Solution
Check Mark

Explanation of Solution

Given Information: The array is A[]={13,19,9,5,12,8,7,4,11,2,6,21}.

Explanation: According to the HOARE-PARTITION, algorithm p=1 and r=12 so the while loop run from 1 to 12.

Consider that x=13 is pivot element.

Now the array and auxiliary after each pass through HOARE-PARTITION algorithm will show in below table-

Introduction to Algorithms, Chapter 7, Problem 1P , additional homework tip  1

After the iteration p=4 , the elements which is greater than pivot element( x=13 ) is placed in their suitable place then the partition pass stop and show the final result of partition as shown in below-

Introduction to Algorithms, Chapter 7, Problem 1P , additional homework tip  2

Hence, the HOARE-PARTITION algorithm exit the while loop with the auxiliary values of i=10 and j=2 .

(b)

Program Plan Intro

To showthat the indices i and j are such that we never access an element of array A outside the sub-array.

(b)

Expert Solution
Check Mark

Explanation of Solution

In the beginning of while loop when |A|2 then the condition i>j will be true.

Consider that it has an element k and k>i that means that A[k]x and k'<j that is A[j']x this proves that I and j are outside the bounds of array and element x must lies in between the two as i>j

If it takes parameter k=j and k'=i then the element j was in the position of element i in the next iterations of loop that proves that the element k fulfill the relation of x that is A[k]x .

Hence, it is can be concluded that the indices i and j are such that one never access an element of array A outside the sub-array.

(c)

Program Plan Intro

To explain that when HOARE-PARTITION terminates, it returns a value j such that pj<r .

(c)

Expert Solution
Check Mark

Explanation of Solution

The value of jis decreased after every iteration and at the final iteration of the loop i will equals to 1but greater than r .

Line 11 of HOARE-PARTITION illustrate that i=1 because in the array A[p]=xx so the HOARE-PARTITION algorithm terminates and at that time j=1 and p>1 .

Therefore, the HOARE-PARTITION algorithm terminates and returns j=1>p .

(d)

Program Plan Intro

To explain that the every element of array is less than or equal to every element of A[j+1...r] , when HOARE-PARTITION algorithm terminates.

(d)

Expert Solution
Check Mark

Explanation of Solution

Consider that it just finished the iteration of the loop in which j went to j1 and j2 and I went to i1 to i2 . The elements of the array A[i+1......i1] are less than x as defined in the algorithm from line 8 to line 10.

Similarly, the elements of the array A[j+1....j1] are less than x as defined in the algorithm so the condition of array will be A[i]xA[j] after the exchange of line 12. Since all the elements of A[j......r] is greater than or equal to x.

Now putting all the conditions of the array defined in the algorithm it has condition that is A[p......i]=A[p.....i1]A[p.....i2]........A[p....r] .

Since at the termination of the algorithm ij which implies that A[p...j]A[p...i] that means that every elements of array A[p..j] is less than or equal to x and x is less than or equal to the every element of A[j+1...r]A[j...r] .

Therefore, the every element of array A[p..j] is less than or equal to every element of A[j+1...r] when HOARE-PARTITION algorithm terminates.

(e)

Program Plan Intro

To rewrite the QUICK-SORT procedure by using HOARE-PARTITION algorithm.

(e)

Expert Solution
Check Mark

Explanation of Solution

QUICKSORT(A,p,r).
	If   then
		q =HOARE-PARTITION(A,p,r).
		QUICKSORT(A,p,q-1).
		QUICKSORT(A,q+1,r).
	End if.

HOARE-PARTITION(A,p,r)
	 
	while TRUE
		repeat
			 
		until .
		repeat 
			 .
		until .
		if  then
			exchange  with .
		else return j.
		end if.
	end while.

The quick sort algorithm is based on recursion. It first sets the parameters and then check the initial condition.

If the initial condition is true then it call the HOARE-PARTITION algorithm with recursion of calling itself again and again until the initial condition becomes false.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Edit the Person.java file so that the natural ordering or a Person is by last name in ascending order. Use the merge sort algorithm to implement the MergeSort sort method.   import java.util.ArrayList; import java.util.List;   public class Main {     public static void main(String[] args) {         List<Person> people = new ArrayList<>();           people.add(new Person("Frank", "Denton", 73));         people.add(new Person("Mark", "Cohen", 44));         people.add(new Person("Tim", "Smith", 22));         people.add(new Person("Steve", "Denton", 16));         people.add(new Person("Andy", "Ashton", 44));         people.add(new Person("Albert", "Denton", 58));         System.out.println("Before: " + people);           List<Person> result = MergeSort.sort(people);         System.out.println("After: " + result);     } }   import java.util.Collections; import java.util.List;   public class MergeSort {     public static List<Person> sort(List<Person> people) {…
The partitioning algorithm during quick-sort normally runs ______ (many times, exactly once)
Create a sort implementation that counts the variety of key values before sorting the array using key-indexed counting using a symbol table. (This approach should not be used if there are many distinct key values.)
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    Systems Architecture
    Computer Science
    ISBN:9781305080195
    Author:Stephen D. Burd
    Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning