Question

Illustrate the operation of PARTITION on the array ? = < 15, 1,9, 5, 13,6, 8,...

Illustrate the operation of PARTITION on the array ? = < 15, 1,9, 5, 13,6, 8, 4, 21, 2, 6, 11 >

Following is the algorithm we used in class:

PARTITION(A, p, r)

1 x = A[r]

2 i = p -1

3 for j = p to r -1

4 if A[j] ≤ x

5 i = i +1

6 exchange A[i] with A[j]

7 exchange A[i+1] with A[r]

8 return i + 1

**Please use p,j,i, and r on the array so I can clearly see the steps to better understand. Any help is appreciated. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

? = < 15, 1,9, 5, 13,6, 8, 4, 21, 2, 6, 11 >

Initially p = 1 and r = 12 [I.e size]
Line 1. x = A[r] i.e x = A[12] = 11 i.e last value in array

Line 2. i = p-1 i.e i = 0
Line 3. Loop starts from j = 1 to 11 (r-1) [ Last element is omitted]
----------------if A[1] i.e 15 < 11 false , i remains as 0
----------------if A[2] i.e 1 < 11 true , i is incremented by 1 i.e its 1 now
------------------------------------Swap A[1] and A[2] i.e [ 1, 15,
9, 5, 13,6, 8, 4, 21, 2, 6, 11 ]
----------------if A[3] i.e 9 < 11 true , i is incremented by 1 i.e its 2 now
------------------------------------Swap A[2] and A[3] i.e [
1, 9,15, 5, 13,6, 8, 4, 21, 2, 6, 11 ]
----------------if A[4] i.e 5 < 11 true , i is incremented by 1 i.e its 3 now
------------------------------------Swap A[3] and A[4] i.e
[ 1, 9,5, 15, 13,6, 8, 4, 21, 2, 6, 11 ]
----------------if A[5] i.e 13 < 11 false , i remains as 3
----------------if A[6] i.e 6 < 11 true , i is incremented by 1 i.e its 4 now
------------------------------------Swap A[4] and A[6] i.e
[ 1, 9,5, 6, 13,15, 8, 4, 21, 2, 6, 11 ]
----------------if A[7] i.e 8 < 11 true , i is incremented by 1 i.e its 5 now
------------------------------------Swap A[5] and A[7] i.e
[ 1, 9,5, 6, 8,15, 13, 4, 21, 2, 6, 11 ]
----------------if A[8] i.e 4 < 11 true , i is incremented by 1 i.e its 6 now
------------------------------------Swap A[6] and A[8] i.e
[ 1, 9,5, 6, 8,4, 13, 15, 21, 2, 6, 11 ]
----------------if A[9] i.e 21 < 11 false , i remains as 6
----------------if A[10] i.e 2 < 11 true , i is incremented by 1 i.e its 7 now
------------------------------------Swap A[7] and A[10] i.e
[ 1, 9,5, 6, 8,4, 2, 15, 21, 13, 6, 11 ]
----------------if A[11] i.e 6 < 11 true , i is incremented by 1 i.e its 8 now
------------------------------------Swap A[8] and A[11] i.e
[ 1, 9,5, 6, 8,4, 2, 6, 21, 13, 15, 11 ]


Now Since the LOOP IS COMPLETE IT WILL EXCHANGE [i+1] with 11

[ 1, 9,5, 6, 8,4, 2, 6, 11, 21, 13, 15, 21 ]

Now we can see that elements to left of 11 are less and elements to right of 11 are greater ..Hence it partitions the Array.

PLEASE UPVOTE, Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Illustrate the operation of PARTITION on the array ? = < 15, 1,9, 5, 13,6, 8,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 5 (10 points) Below is the PARTITION(A, p. r) pseudocode where pivot is always selected...

    Question 5 (10 points) Below is the PARTITION(A, p. r) pseudocode where pivot is always selected as the last element and array A=(-23,7,-14,1,5,1). a. Illustrate the PARTITIONCA, P.7) module operation like below when call it with the above array A. Show clearly the position of i and ; in each picture. Pj PARTITIONCA, p,r) 1. x=A[r] 2. i=p - 1 3. for j =p tor-1 4. if Allsx 5. i=i+1 exchange A[i] with AD 7. exchange A[i+1] with 4[r] 8....

  • Illustrate the operation of randomized quicksort on the array: A = (19, 2, 11, 14, 7,...

    Illustrate the operation of randomized quicksort on the array: A = (19, 2, 11, 14, 7, 17, 4, 3, 5, 15) by showing the values in A after each call to partition. Assume that the randomly chosen pivots were, in order, 〈19, 14, 2, 5, 4, 7, 15〉.

  • Use Figure 6.4 as a model, illustrate the operation of HEAPSORT on the array A =...

    Use Figure 6.4 as a model, illustrate the operation of HEAPSORT on the array A = <4, 10, 7, 25, 8, 3>. Show all intermediate steps how the heap is transformed. 6.4/ 91|11|016 | 8|4|||7|V (9) 09

  • 6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2)...

    6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while i<=j do for a=i to j by i do 4: 5: 6: 7: if Pla>Pat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end...

  • you will analyse two algorithms for finding the median of an array of integers. You will...

    you will analyse two algorithms for finding the median of an array of integers. You will compare both algorithms in terms of timing, and hopefully design a hybrid algorithm that uses both, depending on input size. You will write a report describing your experiments and results. the following Java program implements two algorithms for finding the median of an array of integers. The first uses merge sort, and the other implements the recursive linear time selection algorithm, the task is...

  • Algorithm Illustrate the HeapSort on the following array. [ 7, 2, 4, 6, 3, 1, 5...

    Algorithm Illustrate the HeapSort on the following array. [ 7, 2, 4, 6, 3, 1, 5 ] Show the array and the heap after each call to ReHeap.

  • Data Structures and Algorithm Analysis illustrate the result of each operation in the sequence PUSH(S, 4),...

    Data Structures and Algorithm Analysis illustrate the result of each operation in the sequence PUSH(S, 4), PUSH(S, 2), POP(), PUSH(S, 3), PUSH(S, 9), POP(), and PUSH(S, 0) on an initially empty stack S stored in array S[1..6]. 1 2 3 4 5 6 7 1 2 3 4 5 6 7 15 6 2 9 17 3 S15 6 29 173 top[S] = 4 top[S] = 6 top[S] = 5

  • Hi, I have programming problem related to array, sorting, and. swap operation Thank you, Best Regards.....

    Hi, I have programming problem related to array, sorting, and. swap operation Thank you, Best Regards.. A non-empty array A consisting of N integers is given. You can perform a single swap operation in array A. This operation takes two indices I and J, such that 0 S13 J<N, and exchanges the values of A[i] and A[J]. The goal is to check whether array A can be sorted into non-decreasing order by performing at most one swap operation. For example,...

  • 1. What is the output of the following code segment? int array[] = { 8, 6,...

    1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; System.out.println( "Index Value" ); for ( int i = 0; i < array.length; i++ ) System.out.printf( "%d %d\n", i, array[ i ] ); 2. What is the output of the following code segment? char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; String output = "The sentence...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT