Introduction To Algorithms, Third Edition (international Edition)
Introduction To Algorithms, Third Edition (international Edition)
3rd Edition
ISBN: 9780262533058
Author: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein
Publisher: TRILITERAL
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, Third Edition (international Edition), 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, Third Edition (international Edition), 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
The next problem concerns the following C code: /copy input string x to buf */ void foo (char *x) { char buf [8]; strcpy((char *) buf, x); } void callfoo() { } foo("ZYXWVUTSRQPONMLKJIHGFEDCBA"); Here is the corresponding machine code on a Linux/x86 machine: 0000000000400530 : 400530: 48 83 ec 18 sub $0x18,%rsp 400534: 48 89 fe mov %rdi, %rsi 400537: 48 89 e7 mov %rsp,%rdi 40053a: e8 d1 fe ff ff 40053f: 48 83 c4 18 add callq 400410 $0x18,%rsp 400543: c3 retq 0000000000400544 : 400544: 48 83 ec 08 sub $0x8,%rsp 400548: bf 00 06 40 00 mov $0x400600,%edi 40054d: e8 de ff ff ff callq 400530 400552: 48 83 c4 08 add $0x8,%rsp 400556: c3 This problem tests your understanding of the program stack. Here are some notes to help you work the problem: • strcpy(char *dst, char *src) copies the string at address src (including the terminating '\0' character) to address dst. It does not check the size of the destination buffer. You will need to know the hex values of the following characters:
A ROP (Return-Oriented Programming) attack can be used to execute arbitrary instructions by chaining together small pieces of code called "gadgets." Your goal is to create a stack layout for a ROP attack that calls a function located at '0x4018bd3'. Below is the assembly code for the function 'getbuf', which allocates 8 bytes of stack space for a 'char' array. This array is then passed to the 'gets' function. Additionally, you are provided with five useful gadgets and their addresses. Use these gadgets to construct the stack layout. Assembly for getbuf 1 getbuf: 2 sub $8, %rsp 3 mov %rsp, %rdi 4 call gets 56 add $8, %rsp ret #Allocate 8 bytes for buffer #Load buffer address into %rdi #Call gets with buffer #Restore the stack pointer #Return to caller. Stack Layout (fill in Gadgets each 8-byte section) Address Gadget Address Value (8 bytes) 0x4006a7 pop %rdi; ret 0x7fffffffdfc0 Ox4006a9 pop %rsi; ret 0x7fffffffdfb8 0x4006ab pop %rax; ret 0x7fffffffdfb0 0x7fffffffdfa8 Ox4006ad mov %rax,…
In each of the following C code snippets, there are issues that can prevent the compilerfrom applying certain optimizations. For each snippet:• Circle the line number that contains compiler optimization blocker.• Select the best modification to improve optimization.1. Which line prevents compiler optimization? Circle one: 2 3 4 5 6Suggested solution:• Remove printf or move it outside the loop.• Remove the loop.• Replace arr[i] with a constant value.1 int sum( int ∗ ar r , int n) {2 int s = 0 ;3 for ( int i = 0 ; i < n ; i++) {4 s += a r r [ i ] ;5 p r i n t f ( ”%d\n” , s ) ;6 }7 return s ;8 }2. Which line prevents compiler optimization? Circle one: 2 3 4 5 6Suggested solution:• Move or eliminate do extra work() if it’s not necessary inside the loop.• Remove the loop (but what about scaling?).• Replace arr[i] *= factor; with arr[i] = 0; (why would that help?).1 void s c a l e ( int ∗ ar r , int n , int f a c t o r ) {2 for ( int i = 0 ; i < n ; i++) {3 a r r [ i ] ∗= f a c t o r…
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