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
23:12 Chegg content://org.teleg + 5G 5G 80% New question A feed of 60 mol% methanol in water at 1 atm is to be separated by dislation into a liquid distilate containing 98 mol% methanol and a bottom containing 96 mol% water. Enthalpy and equilibrium data for the mixture at 1 atm are given in Table Q2 below. Ask an expert (a) Devise a procedure, using the enthalpy-concentration diagram, to determine the minimum number of equilibrium trays for the condition of total reflux and the required separation. Show individual equilibrium trays using the the lines. Comment on why the value is Independent of the food condition. Recent My stuff Mol% MeOH, Saturated vapour Table Q2 Methanol-water vapour liquid equilibrium and enthalpy data for 1 atm Enthalpy above C˚C Equilibrium dala Mol% MeOH in Saturated liquid TC kJ mol T. "Chk kot) Liquid T, "C 0.0 100.0 48.195 100.0 7.536 0.0 0.0 100.0 5.0 90.9 47,730 928 7,141 2.0 13.4 96.4 Perks 10.0 97.7 47,311 87.7 8,862 4.0 23.0 93.5 16.0 96.2 46,892 84.4…
You are working with a database table that contains customer data. The table includes columns about customer location such as city, state, and country. You want to retrieve the first 3 letters of each country name. You decide to use the SUBSTR function to retrieve the first 3 letters of each country name, and use the AS command to store the result in a new column called new_country.  You write the SQL query below. Add a statement to your SQL query that will retrieve the first 3 letters of each country name and store the result in a new column as new_country.
We are considering the RSA encryption scheme. The involved numbers are small, so the communication is insecure.  Alice's public key (n,public_key) is (247,7). A code breaker manages to factories  247 = 13 x 19  Determine Alice's secret key. To solve the problem, you need not use the extended Euclid algorithm, but you may assume that her private key is one of the following numbers 31,35,55,59,77,89.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L