0% found this document useful (0 votes)
13 views3 pages

Lec 6

DOA Lec-6

Uploaded by

Sake Anila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Lec 6

DOA Lec-6

Uploaded by

Sake Anila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS212: Design & Analysis of Algorithms 3rd Semester

Lecture 6: August 14
Instructor: Prof. Prateek Vishnoi Indian Institute of Technology, Mandi

In the previous class, we have seen a sorting algorithm(Merge Sort) that has time complexity Θ(n log n) and
space complexity O(n). We now look another sorting algorithm in the hope of Better.

Quick Sort
Depends on divide and conquer approach.

It is called Inplace sorting as it doesn’t use an extra array.

Steps of Quick Sort


Select last element of an array as pivot element.
Initialise pointer j as 1st (base address) index of an array.
Initalise pointer i which points to the address just before the base address of an array.

Iterate the pointer j over an array with following conditions:


ˆ If value at pointer j is ≥ pivot, IGNORE.
ˆ If value at pointer j < pivot, then i++ and swap(value at i , value at j)
ˆ Recursively call the Quick Sort Function on left subarray of pivot element.
ˆ Recursively call the Quick Sort Function on right subarray of pivot element.

Example of Quick Sort

Check the slides where I showed the complete steps of quick sort with the help of an example.

6-1
6-2 Lecture 6: August 14

Quick Sort Pseudocode

Algorithm 1 Quick Sort


1: function QuickSort(array, low, high)
2: if low < high then
3: pivotIndex = Partition(array, low, high)
4: QuickSort(array, low, pivotIndex - 1)
5: QuickSort(array, pivotIndex + 1, high)
6: end if
7: end function
8: function Partition(array, low, high)
9: pivot = array[high]
10: i = low - 1
11: for j = low to high - 1 do
12: if array[j] ≤ pivot then
13: i=i+1
14: Swap(array[i], array[j])
15: end if
16: end for
17: Swap(array[i + 1], array[high])
18: return i + 1
19: end function
20: function Swap(a, b)
21: temp = a
22: a=b
23: b = temp
24: end function

General Recurrence Relation

(
T (k) + T (n − k − 1) + cn if n > 1
T (n) =
c if n = 1

Time Complexity Analysis


Best Case : (
2T ( n2 ) + cn if n > 1
T (n) =
c if n = 1

Time Complexity = Ω(n log n)

Worst Case : (
T (n − 1) + cn if n > 1
T (n) =
c if n = 1

Time Complexity = O(n2 )


Lecture 6: August 14 6-3

Average Case : (
T (an) + T ((1 − a)n) + cn if n > 1
T (n) =
c if n = 1
where 0 < a < 1 is a constant representing the average split ratio.

Regardless of the exact value of a solving this recurrence relation still gives:

Time Complexity = O(n log n)

Worst Case Analysis

Worst case behavior occurs for the sorted array. Since we are using the last element of array as pivot, parti-
tion of array puts all elements in either at left or right.

In order to deal with it, we introduced the randomness on selecting the pivot element. We select the pivot
element uniformly at random among all possiblities. This new modified algorithm is called Randomised
Quick Sort.

However, the worst case behavior got reduced for many instances but still there are instances where time
complexity blow up to O(n2 ).

This can be easily checked that for any array of form [a,a,a,a . . . a], even randomised quick sort takes O(n2 )
time.

Space Complexity

We are not utilizing any additional space; therefore, the space we are utilizing is solely of the stack capacity.
The worst-case scenario is O(n), while the average case is O(log n).

Judgement Question
Is it possible to alter the randomized quick sort algorithm in such a way that the worst-case time
complexity becomes O(n log n)?

Is quick sort a stable sort? Prove or disprove.

You might also like