Sorting II - Part I
Sorting II - Part I
1
Learning Objectives
● Understand the basic principles and algorithms behind merge sort and quicksort.
● Compare and contrast the time complexity of merge sort, quick sort, bucket sort,
and cyclic sort, including best-case, worst-case, and average-case scenarios.
● Identify the strengths and weaknesses of each sorting algorithm in terms of stability,
adaptability to different data distributions, and ease of implementation.
● Explore potential optimizations and variations of merge sort, quick sort, bucket sort,
and cyclic sort, such as parallelization, hybrid algorithms, and memory
management techniques.
2
Lecture Flow
1) Pre-requisites
2) Revision
3) Part I
● Merge Sort
● Bucket Sort
4) Part II
● Quick Sort
● Cyclic Sort
5) Practice Questions
6) Resources
7) Quote of the Day
3
Pre-requisites
● Sorting - Basics
● Asymptotic Analysis
● Arrays
● Willingness to learn
4
Revision
5
Bubble Sort
6
Time & Space Complexity
7
Time & Space Complexity
8
Selection Sort
9
Time & Space Complexity
10
Time & Space Complexity
11
Insertion Sort
12
Time & Space Complexity
13
Time & Space Complexity
14
Counting Sort
15
Time complexity
16
Time complexity
Note: Counting sort is most efficient if the range of input values is not greater
than the number of values to be sorted.
Time to get efficient !
18
Part I
19
Merge Sort
20
Merge Sort
21
Merge Sort
22
Q: Can you guess the next set of moves in the following dance ?
23
Divide
Conquer
Divide and Conquer
● Sort both halves with merge
sort
● Base case?
Combine
24
Practice
25
Can you implement the function merge ?
Implement Here
26
Implementation
def merge(left_half, right_half):
left_index = 0
right_index = 0
sorted_subarray = []
sorted_subarray.extend(left_half[left_index:])
sorted_subarray.extend(right_half[right_index:])
return sorted_subarray
27
Implementation
def mergeSort(left, right, arr):
if left == right:
return [arr[left]]
mid = left + (right - left) // 2
left_half = mergeSort(left, mid, arr)
right_half = mergeSort(mid + 1, right, arr)
28
Q: Is Merge Sort a Stable Sorting Algorithm ?
?
29
Q: What do you think is the time complexity for the aforementioned
sorting Algorithm?
?
30
Time & Space Complexity
31
Time & Space Complexity
32
Pair Programming
Question 1
33
Bucket Sort
34
Bucket Sort
35
Bucket Sort
36
Bucket Sort
Problem:
Sort a large set of floating point numbers which are in range from 0.0 to 1.0
and are uniformly distributed across the range. How do we sort the numbers
efficiently?
37
Bucket Sort
Approach:
bucket_sort(arr[], n)
38
Bucket Sort
39
Visualization Link
40
Can you implement the function bucket_sort ?
Implement Here
41
Vanilla Implementation
def bucketsort(arr, n):
buckets = [[] for _ in range(n + 1)]
_min= min(arr)
ans = []
_range = max(arr) - _min
if _range == 0:
return arr
42
Time & Space Complexity
43
Time & Space Complexity
44
Pair Programming
Question 2
45
Practice Problems
Sort List
Masha and Beautiful Tree
Count of Smaller Numbers After Self
Number of Pairs Satisfying Inequality
Create Sorted Array through Instructions
46
Quote of the Day
47