You are on page 1of 41

Prepared by Julian García, based on material by

David Albrecht and María García de la Banda

Lecture 24
Recursive sorting II
FIT 1008&2085
Introduction to Computer Science

COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Cop yright Act 1968 (the Act).The material in this communication may be subject to copyright under the Act.Any further reproduction o
communication of this material by you may be the subject of copyright protection under the Act.
Do not remove this notice.
Overview
• To review what a “divide and conquer” algorithm is

• To review in more depth two different “divide and


conquer” sorting algorithms:
• Merge Sort
• Quick Sort

• To be able to implement them and compare their


efficiency for different classes of inputs

2
Divide and Conquer: Sorting

Divide the array into 2 parts

Sort the first part

Sort the second part

Combine
3
Divide and Conquer: Sorting
General Idea

• Merge Sort has a simple split and a elaborate combine

• Quick Sort has a elaborate split and a simple combine


4
Quicksort
Top-10 algorithms 20th century (SIAM)

5
Quick Sort
• Partition the list
• Sort the first part (recursively)
• Sort the second part (recursively)
• (Combine: there is nothing to do!)

6
Partition
• Choose an item in the list, called it the pivot.
• The first part consists of all those items which
are less than the pivot.
• The second part consists of all those items
larger than or equal to the pivot (except the
pivot). so we need to do some shuffling around to organise the data after we have chosen a pivot

7
after doing the partition we have all the data less than the pivot, the pivot, then all the data greater than the pivot then we recursively sort each section based on this partition.
why the combination step is trivial as every element in the first section of the partition is smaller than every element in the second section of the partition; this renders the merge operation as trivial.

Partition
x<p p p <= x
Sort should read x >= p
Sort

• Partition: Elaborate, based on a pivot p.

• Combination: Simple append, pivot in the middle.


8
Ideally, what should the pivot be?
D - the middle element of the output list as the output list is sorted therefore making the middle element of that list the median
the reason why the input list is undesireable is because we will be shuffling items around
the median is chosen so that we have a divide by 2 division giving us a logarithmic time complexity.

A) The smallest element of the list.

B) The largest element of the list.

C) The middle element of the input list.

D) The middle element of the output list.

E) Something else.

F) It actually makes no difference.

9
Given a list of size N, how efficiently can the median be
computed?

A) O(1)

B) O(log N) the calculate the median element of a list it is a O(N) operation as we need to look at each part of the data in the input set

C) O(N)

D) O(N log N)
In practise we might not pick exactly the median each time we want to divide as this will be come costly
instead we can choose an element that will give approximately.a half-and-half split
thus we can pick the median of a subset of the lists elements

E) O(N^2)

11
Example Partition

array: 5 89 35 14 24 15 37 13 20 7 70

start:0 end:10

13
Example Partition

array: 5 89 35 14 24 15 37 13 20 7 70

Randomly choose a pivot, which


happens to be in the middle

14
Example Partition

array: 5 89 35 14 24 15 37 13 20 7 70

partition:
5 89 35 14 24 15 37 13 20 7 70

result
7 14 5 13 15 35 37 89 20 24 70

pivot position: 4
note that the pivot defines the boundaries

sort first half (using QS), 15sort second half (using QS)
Quicksort

16
Quicksort

17
p
swap with first element

p
index k

p x<p p >= x
index increases if necessary index k always increases

p x<p p >= x

x<p p p >= x

18
Example Partition
randomly pick element in position 5

array: 5 89 35 14 24 15 37 13 20 7 70

15 89 35 14 24 5 37 13 20 7 70

19
Example Partition

15 89 35 14 24 5 37 13 20 7 70

k:1

20
Example Partition
index:0

15 89 35 14 24 5 37 13 20 7 70

k:1

21
Example Partition
index:0

15 89 35 14 24 5 37 13 20 7 70

k:2

22
Example Partition
index:0

15 89 35 14 24 5 37 13 20 7 70

k:3

23
Example Partition
index:1

15 14
89 35 89
14 24 5 37 13 20 7 70

k:3

24
Example Partition
index:1

15 89
14 35 89 24 5 37 13 20 7 70

k:4

25
Example Partition
index:1

15 89
14 35 89 24 5 37 13 20 7 70

k:5

26
Example Partition
index:2

15 89
14 5 89 24 35 37 13 20 7 70

k:5

27
Example Partition
index:2

15 89
14 5 89 24 35 37 13 20 7 70

k:6

28
Example Partition
index:2

15 89
14 5 89 24 35 37 13 20 7 70

k:7
etc…

29
Example Partition
index:4

15 89
14 5 13 7 35 37 89 20 24 70

k:11

30
Example Partition
index:4

15
7 89
14 5 13 15
7 35 37 89 20 24 70

x < 15 x >= 15

After last swap, pivot is in the


correct position

31
Example Partition

7 14 5 13 15 35 37 89 20 24 70

quick_sort quick_sort
7 5 13 14 20 24 35 37 70 89

32
p
swap with first element

p
index k

p x<p p >= x
index

p x<p p >= x

x<p p p >= x

33
34
37
Found an element
that belongs in 1…
index
swap and update
index to maintain
invariant

38
What is the time complexity of the partition method?

A) O(log N)

B) O(N)

C) O(N log N)

D) O(N2)

39
What is the best-case time complexity of quicksort?

A) O(log N)

B) O(N)

C) O(N log N)

D) O(N2)

41
Quicksort: Number of partitions depends on the pivot

p
Partition
x<p p p <= x

pivot

Best case: The size of the problem is reduced by half with


every partition
Worst case: The size of the problem is reduced by 1 with
every partition 43
Quick sort’s best case partition is O(n)
n
n

n
height n/2 n/2

n
is
n/4 n/4 n/4 n/4
O(log
n
n)
n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

Running time in the best case: O(n log n)

44
Quick sort’s worst case

n
partition is O(n) n
n-1
n-1
n-2
n-2
n-3
n-3

...
n-4

n (n+1)
_______
2

Running time in the worst case: O(n2)

45
Summary

Best case Worst case

Quicksort O(n log n) O(n2)

Mergesort O(n log n) O(n log n)

How common is quicksort’s worst case?


Not too common if choosing a random pivot.
51
Summary
• Divide and Conquer and Recursive Algorithms
(for sorting).

• Merge Sort
• Easy: Split
• Elaborate: merge method

• Quick Sort
• Elaborate split: partition method
• Easy combination
52

You might also like