You are on page 1of 10

Recursive Algorithm

Example
Quick sort complexity
Recursive Algorithm cont…
QUICKSORT(A, p, r): A: array,p: LB,r=UB
1. if p < r
2. q =PARTITION(A, p, r)
3. QUICKSORT(A, p, q-1)
4. QUICKSORT(A, q +1, r)
5. Exit
Recursive Algorithm(partition)
PARTITION(A, p, r)
1. x = A[r]// pivot is last value
2. i = p-1
3. Repeat steps 4 to 6 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
Example:
Complexity of Quick sort
Worst case
O(n2)

Average case
O(n log n)

Best case
 O(n log n)
BRAINSTORMING

Given, ARR = {7, 4, 6, 5, 3, 4}

What will be the Array after applying the first PASS of


Quick Sort (to sort in Ascending Order)?

A. 3, 4, 4, 5, 7, 6
B. 4, 3, 4, 5, 7, 6
C. 3, 4, 4, 5, 6, 7
D. None of These
• Answer: B

Ravi Kant Sahu, Asst. Professor @ Lovely


Professional University, Punjab (India)
BRAINSTORMING

Given, ARR = {7, 4, 6, 5, 3, 4}

What will be the Value of q after the first PASS of Quick


Sort (to sort in Ascending Order)?

[Indexes starts from 0]

A. 1
B. 2
C. 3
D. 4
APPLY QUICK SORT ON THIS:
52, 37, 63, 14, 17, 8, 6, 25
Thank You

You might also like