0% found this document useful (0 votes)
50 views

Quick Sort Using Recursion

This document discusses quicksort, a recursive algorithm for sorting a list of elements. Quicksort first selects a pivot element and partitions the list around the pivot, putting elements less than the pivot before it and greater elements after it. It then recursively applies this process to the sublists until the entire list is sorted. The quicksort algorithm runs in O(n log n) time on average but can degrade to O(n^2) time in the worst case if the pivot selections are poor.

Uploaded by

Avinash Kr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Quick Sort Using Recursion

This document discusses quicksort, a recursive algorithm for sorting a list of elements. Quicksort first selects a pivot element and partitions the list around the pivot, putting elements less than the pivot before it and greater elements after it. It then recursively applies this process to the sublists until the entire list is sorted. The quicksort algorithm runs in O(n log n) time on average but can degrade to O(n^2) time in the worst case if the pivot selections are poor.

Uploaded by

Avinash Kr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Recursive Algorithm

Example
Quick sort complexity
Recursive Algorithm cont…
 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)
5. Exit
Recursive Algorithm(partition)
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
Example:
Complexity of Quick sort
 Worst case
 O(n2)

 Average case
 O(n log n)

 Best case
 O(n log n)
Thank You

You might also like