Solution:: Assignment 2 CS502

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

ASSIGNMENT 2

CS502

Solution:

5 7

25 13 19 12

30 27 26

(b)

30

27 19

25 26 5 12

1 13 7
Question No. 02 (Marks 10)

PART( array B, int p, int r)


1. x ← B[p]
2. q ← p
3. for s ← p + 1 to r
4. do if (B[s] < x)
5. then q ← q + 1
6. swap B[q] with B[s]
7. swap B[p] with B[q]
8. return q

9 1 7 20 12 18 2 15 4 17

9 1 7 20 12 18 2 15 4 17

9 1 7 20 12 18 2 15 4 17

9 1 7 20 12 18 2 15 4 17

9 1 7 20 12 18 2 15 4 17

9 1 7 20 12 18 2 15 4 17

9 1 7 2 12 18 20 15 4 17

9 1 7 2 12 18 20 15 4 17

9 1 7 2 4 18 20 15 12 17

4 1 7 2 9 18 20 15 12 17

QUICKSORT( array B, int p, int r)


1. if (r>p)
2. then
3. i ← a random index from [p….r]
4. swap B[i] with B[p]
5. q ← PARTITION(B,p,r)
6. QUICKSORT(B,p,q-1)
7. QUICKSORT(B,q+1,r)

You might also like