Sorting algorithm question For a given array A 2 4 6 8 0 1

Sorting algorithm question!!

For a given array A = (2, 4, 6, 8, 0, 1, 3, 5) and a partition method below, show the resulting array after making a call to Partition(A, 0, 7). Show each iteration step in detail. PARTITION(A, p, r) 1 x = A[r] 2 i = p - 1 3 for j = p to r - 1 4 if A[j] lessthanorequalto x 5 i = i + 1 6 exchange A [i] with A [j] 7 exchange A[i + 1] with A[r] 8 return i + 1 Trace the execution of test(4). Write it out. void test(int n) {if (n > 0) {System.out.printIn(n); test(n-1); System.out.printIn(n);}}

Solution

2. If you call the function as follows,

step 1 :

test(4)

if ( 4>0 ) // check 4 is greater than 0. it is true so it will print 4.

print 4.

test (3) // recursively call test (3 )

step 2 :

test (3 )

if (3 >0) //true

print 3

test (2 )

step 3 :

test (2)

if 2>0 // true

print 2.

test (1)

step 4 :

test (1)

if 1>0 // true

print 1.

test (0)

step 5 :

test (0)

if 0 > 0 // this is wrong. therefor it will go back to where the function called, and execute the // next styatement followed bt function call

print 0.   

final output is :    4 3 2 1 0

**************END*************PLS GIVE ME GOOD RATING*****************************

Sorting algorithm question!! For a given array A = (2, 4, 6, 8, 0, 1, 3, 5) and a partition method below, show the resulting array after making a call to Partit

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site