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
What is the value of the median-of-3 for the following list. [80, 47, 41, 21, 40, 68, 78, 18, 75, 46, 30] Notes: • Your answer should be a single valid, non-negative, literal Python int value. For example, 123 is a valid int literal. • This question is asking about before any partition operation is carried out. • This is not asking for the median-of-3 index. • You can pre-check your answer (to check it's a valid, non-negative, literal int).
This is possible because the algorithm assumes that each of the n input elements is in the range [0, k], i.e. no element is smaller than 0 nor greater than k. The input is an array A indexed from 1 to n, the output is stored in the array B also indexed from 1 ton, and the array C is used as a temporary container, indexed from 0 to k. 1 2 3 5 6 7 8 10 11 12 let C[0..k] be a new array for i=0 to k C[i] = 0 for j = 1 to A.length C[A[j]] = C[A[j]] + 1 // C[i] now contains the number of elements equal to i. for i=1 to k C[i] = C[i] + C[i-1] // C[i] now contains the number of elements less than or equal to i. for j = A.length downto 1 B[C[A[j]]]= A[j] C[A[j]] = C[A[j]] - 1 Given an input array A = [5, 1, 2, 1, 5, 3], what is the status of C' at the end of the algorithm? ○ 1, 2, 3, 4, 4,6 none of the others O 0, 1, 2, 3, 5,5 O 4, 4, 5, 2,0 O 0, 0, 2, 3, 4, 4
Write the details algorithm and convert into java code for the solution of the following problem In this assignment, you are given a following table. You implement the table as an ADT by following methods. 1- Array based implementation 2- Reference based implementation 3- Binary search tree-based implementation You implement following TABLE ADT operations a) Insert a new item into a table b) Delete the item with a given search key from a table c) Retrieve the item with a given search key from a table City Country Population Athens Greece 2,500,000 Barcelona Spain 1,800,000 Cairo Egypt 9,500,000 London England 9,400,000 New York U.S.A. 7,300,000 Paris France 2,200,000 Rome Italy 2,800,000 Toronto Canada 3,200,000 Venice Italy 300,000
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education