1,534 views
0 votes
0 votes
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

 

illustrate the operation of PARTITION on the array $A=\langle 13,19,9,5,12,8,7,4,21,2,6,11\rangle$

1 Answer

0 votes
0 votes
[13,19,9,5,12,8,7,4,21,2,6,11] //Initially

[9,19,13,5,12,8,7,4,21,2,6,11]

[9,5,13,19,12,8,7,4,21,2,6,11]

[9,5,8,19,12,13,7,4,21,2,6,11]

[9,5,8,7,12,13,19,4,21,2,6,11]

[9,5,8,7,4,13,19,12,21,2,6,11]

[9,5,8,7,4,2,19,12,21,13,6,11]

[9,5,8,7,4,2,6,12,21,13,19,11]

[9,5,8,7,4,2,6,11,21,13,19,12]

Related questions

0 votes
0 votes
1 answer
1
akash.dinkar12 asked Jun 27, 2019
1,705 views
What value of $q$ does PARTITION return when all elements in the array $A[p..r]$ have the same value? Modify PARTITION so that $q=\lfloor(p+r)/2 \rfloor$ when all element...
0 votes
0 votes
1 answer
3
akash.dinkar12 asked Jun 28, 2019
351 views
Why do we analyze the expected running time of a randomized algorithm and not its worst-case running time?
0 votes
0 votes
1 answer
4
akash.dinkar12 asked Jun 27, 2019
926 views
QUICKSORT(A,p,r) 1 if p < r 2 q = PARTITION(A,p,r) 3 QUICKSORT(A, p , q-1) 4 QUICKSORT(A, q + 1, r)How would you modify QUICKSORT to sort into nonincreasing order?