You are on page 1of 37

IT2153/IT2352/IT2553/IT2653/IT2852

Data Structures & Algorithms


Topic 06: Advanced Sort
version 1.0

(Content adapted from:


• Data Structures & Algorithms using Python. Rance D. Necaise, Wiley, 1st
Edition, 2011.
• Data Structures & Algorithms in Python. Michael T. Goodrich, Roberto
Tamassia & Michael H. Goldwasser, Wiley, 1st Edition, 2013.)

Topic 06: Advanced Sort AY2022/23 S1


Sorting Algorithms
 Basic Sort (Topic 04)
 Bubble Sort
 Selection Sort
 Insertion Sort

 Advanced Sort (Topic 06)


 Merge Sort
 Quick Sort

Topic 06: Advanced Sort AY2022/23 S1 2


Merge Sort
 Merge Sort is a sorting algorithm based
on the divide-and-conquer strategy:
 Recursively breaking down a problem into
two or more sub-problems, until these
become simple enough to be solved
directly.
 The solutions to the sub-problems are then
combined to give a solution to the original
problem.

Topic 06: Advanced Sort AY2022/23 S1 3


Merge Sort
 Merge Sort on an input list S with n
elements consists of three steps:
 Divide: partition S into two sublists S1 and
S2 of about n/2 elements each

 Recur: recursively sort S1 and S2

 Conquer: merge S1 and S2 into a unique


sorted list

4
Topic 06: Advanced Sort AY2022/23 S1
Merge Sort
 DIVIDE: Recursively splitting a list until
each element is contained within its own list:

5
Topic 06: Advanced Sort AY2022/23 S1
Merge Sort
 CONQUER: The sublists are merged back
together to create a sorted list:

6
Topic 06: Advanced Sort AY2022/23 S1
Merge Sort Tree
 An execution of Merge Sort is depicted by a binary tree
 each node represents a recursive call of Merge Sort and stores
 unsorted list before the execution and its partition
 sorted list at the end of the execution
 the root is the initial call
 the leaves are calls on sublists of size 0 or 1

7 2  9 4 → 2 4 7 9

7  2 → 2 7 9  4 → 4 9

7→7 2→2 9→9 4→4


7
Topic 06: Advanced Sort AY2022/23 S1
Execution Example
 Partition
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6

7 2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

8
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Recursive call, partition
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6

7 2 → 2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

9
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Recursive call, partition
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

10
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Recursive call, base case
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

11
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Recursive call, base case
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

12
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

13
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Recursive call, …, base case, merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

14
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 8 6

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

15
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Recursive call, …, merge, merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

16
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9

7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8

72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6

7→7 2→2 9→9 4→4 3→3 8→8 6→6 1→1

17
Topic 06: Advanced Sort AY2022/23 S1
Merge Sort
# Sorts a Python list in ascending order using
# the merge sort algorithm
def mergeSort( theList ):
# Check the base case - the list contains a single item
if len(theList) <= 1:
return theList
else:
# Compute the midpoint
mid = len(theList) // 2

# Split the list and perform the recursive step


leftHalf= mergeSort( theList[ :mid ] )
rightHalf = mergeSort( theList[ mid: ] )

# Merge the two sorted sublists


newList = mergeSortedLists( leftHalf, rightHalf)
return newList
18
Topic 06: Advanced Sort AY2022/23 S1
I m plem entation?
 P ractical
Ex ercise

Merging
Two
Sorted
Lists

N OTE: a and
b are index
variables
indicating the
next value to
be merged
from the
respective list.
19
Topic 06: Advanced Sort AY2022/23 S1
Quick Sort
 Quick-sort is also a sorting
algorithm based on the x
divide and conquer
strategy:
 Divide: pick a random
element x (called pivot) and
x
partition S into
 L elements less than x
 E elements equal x L E G
 G elements greater than x
 Recur: sort L and G
 Conquer: join L, E and G x

Topic 06: Advanced Sort AY2022/23 S1 20


Quick Sort
 DIVIDE: Partitions the sequence into segments
based on the pivot value (shown in gray
background):

21
Topic 06: Advanced Sort AY2022/23 S1
Quick Sort
 CONQUER: Merges the sorted segments and
pivot value back into the original sequence:

22
Topic 06: Advanced Sort AY2022/23 S1
Quick-Sort Tree
 An execution of Quick Sort is depicted by a binary tree
 Each node represents a recursive call of Quick Sort and stores
 Unsorted sequence before the execution and its pivot
 Sorted sequence at the end of the execution
 The root is the initial call
 The leaves are calls on subsequences of size 0 or 1

7 4 9 6 2 → 2 4 6 7 9

4 2 → 2 4 7 9 → 7 9

2→2 9→9
23
Topic 06: Advanced Sort AY2022/23 S1
Execution Example
 Pivot selection
7 2 9 43 7 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6

2→2 9 4 → 4 9 3→3 8→8

9→9 4→4

24
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Partition, recursive call, pivot selection
7 2 9 4 3 7 6 1→ 1 2 3 4 6 7 8 9

2 4 3 1→ 2 4 7 9 3 8 6 1 → 1 3 8 6

2→2 9 4 → 4 9 3→3 8→8

9→9 4→4

25
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Partition, recursive call, base case
7 2 9 43 7 6 1→→ 1 2 3 4 6 7 8 9

2 4 3 1 →→ 2 4 7 3 8 6 1 → 1 3 8 6

1→1 9 4 → 4 9 3→3 8→8

9→9 4→4

26
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Recursive call, …, base case, join
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9

2 4 3 1 → 1 2 3 4 3 8 6 1 → 1 3 8 6

1→1 4 3 → 3 4 3→3 8→8

9→9 4→4

27
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Recursive call, pivot selection
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9

2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6

1→1 4 3 → 3 4 8→8 9→9

9→9 4→4

28
Topic 06: Advanced Sort AY2022/23 S1
29
Quick-Sort

Execution Example (cont.)


 Partition, …, recursive call, base case
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9

2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6

1→1 4 3 → 3 4 8→8 9→9

9→9 4→4

29
Topic 06: Advanced Sort AY2022/23 S1
Execution Example (cont.)
 Join, join
7 2 9 4 3 7 6 1 →1 2 3 4 6 7 7 9

2 4 3 1 → 1 2 3 4 7 9 7 → 17 7 9

1→1 4 3 → 3 4 8→8 9→9

9→9 4→4

30
Topic 06: Advanced Sort AY2022/23 S1
Quick Sort (Part 1 of 2)
# Sorts an array or list using the recursive quick sort algorithm
def quickSort( theSeq ):
n = len( theSeq )
recQuickSort( theSeq, 0, n-1 )

# The recursive "in-place" implementation


def recQuickSort( theSeq, first, last ):
# Check the base case (range is trivially sorted)
if first >= last:
return
else:
# Partition the sequence and obtain the pivot position
pos = partitionSeq( theSeq, first, last )

# Repeat the process on the two subsequences


recQuickSort( theSeq, first, pos - 1 )
recQuickSort( theSeq, pos + 1, last )

31
Topic 06: Advanced Sort AY2022/23 S1
Quick Sort (Part 2 of 2)
# Partitions the subsequence using the first key as the pivot
def partitionSeq(theSeq, first, last):
# Save a copy of the pivot value.
pivot = theSeq[first] # first element of range is pivot

# Find the pivot position and move the elements around the pivot
left = first + 1 # will scan rightward
right = last # will scan leftward
while left <= right:
# Scan until reaches value equal or larger than pivot (or right marker)
while left <= right and theSeq[left] < pivot:
left += 1

# Scan until reaches value equal or smaller than pivot (or left marker)
while left <= right and theSeq[right] > pivot:
right -= 1

# Scans did not strictly cross


if left <= right:
# swap values
theSeq[left], theSeq[right] = theSeq[right], theSeq[left]

# Shrink range (Recursion: Progress towards base case)


left += 1
right -= 1

# Put the pivot in the proper position (marked by the right index)
theSeq[first], theSeq[right] = theSeq[right], pivot

# Return the index position of the pivot value.


return right

32
Topic 06: Advanced Sort AY2022/23 S1
partitionSeq()
 First element is the pivot value.
 left marker initialised to the first position following the pivot.
 right marker initialised to the last position within the segment.

 The main loop is executed until one of the two markers crosses the other.
 left marker is shifted to the right until a key value equal or larger than
the pivot is reached, or the left marker crosses the right marker.
 right marker is shifted to the left until a key value equal or lesser than
the pivot is reached, or the right marker crosses the left marker.

33
Topic 06: Advanced Sort AY2022/23 S1
partitionSeq()
 The two keys located at the positions marked by left and
right are then swapped, which will place them within the
proper segment once the location of the pivot is found.

 After the two keys are swapped, the two markers are again shifted
starting from where they left off.

34
Topic 06: Advanced Sort AY2022/23 S1
partitionSeq()
 The left marker will be shifted to key value 51, and the right
marker to value 4.

 Once the two markers are shifted, the corresponding keys are
swapped and the process is repeated

35
Topic 06: Advanced Sort AY2022/23 S1
partitionSeq()
 Next, the left marker will stop at the value 18, while
the right marker will stop at the value 4.

 Note that the right marker has crossed the left


such that right < left  termination of the outer
while loop.

36
Topic 06: Advanced Sort AY2022/23 S1
partitionSeq()
 When the two markers cross, the right marker indicates the final
position of the pivot value in the resulting sorted list.
 Thus, the pivot value (currently located in the first element) and
the element marked by right have to be swapped.

 Finally, the function returns the pivot position for use in splitting
the sequence into two segments.

37
Topic 06: Advanced Sort AY2022/23 S1

You might also like