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
Suppose your computer is responding very slowly to information requests from the Internet. You observe that your network gateway shows high levels of network activity even though you have closed your e-mail client, Web browser, and all other programs that access the Internet. What types of malwares could cause such symptoms? What steps can you take to check whether malware has gained access to your system? What tools can you use at each step? If you identify malware, what ways might it have entered your system? How can you restore your PC to safe operation, including the special software tools you may use?
R language
Using R language
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