Sorting II - Part II (No Code)
Sorting II - Part II (No Code)
1
Part II
2
Quick Sort
3
Quick Sort
4
Quick Sort
5
Quick Sort
Example input
R
P: pivot
W: write index
R: read index
[4, 2, 6, 8, 1, 5, 9]
P W
6
Quick Sort
Example input
R
P: pivot
W: write index
R: read index
[4, 2, 6, 8, 1, 5, 9]
P W
7
Quick Sort
Example input
R
P: pivot
W: write index
R: read index
[4, 2, 6, 8, 1, 5, 9]
P W
8
Quick Sort
Example input
R
P: pivot
W: write index
R: read index
[4, 2, 6, 8, 1, 5, 9]
P W
9
Quick Sort
Example input
R
P: pivot
W: write index
R: read index
[4, 2, 1, 8, 6, 5, 9]
P W
10
Quick Sort
Example input
R
P: pivot
W: write index
R: read index
[4, 2, 1, 8, 6, 5, 9]
P W
11
Quick Sort
Example input
R
P: pivot
W: write index
R: read index
[4, 2, 1, 8, 6, 5, 9]
P W
12
Quick Sort
Example input
R
P: pivot
W: write index
R: read index
[4, 2, 1, 8, 6, 5, 9]
P W
13
Quick Sort
Example input
P: pivot
W: write index
R: read index
[1, 2, 4, 8, 6, 5, 9]
P W
14
Quick Sort
Example input
[1, 2, 4, 8, 6, 5, 9]
15
Visualization Link
16
Can you implement the function partition ?
Implement Here
17
Q: What do you think is the time complexity for the aforementioned
sorting Algorithm?
?
18
Time & Space Complexity
19
Q: what kind of input would result in the worst case?
?
20
What happens in the worst case?
Sorted
array [1, 2, 3, 4, 5]
[2] [3, 4, 5]
[3] [4, 5]
[4] [5]
21
Q: Can we do better ? How ?
?
22
How can we avoid the worst case?
23
Time & Space Complexity
24
Cycle/ Cyclic Sort
25
Cycle Sort
It is known that all comparison-based sorting algorithms have a lower bound time
complexity of Ω(N log N).
26
Cycle Sort
Problem:
You are given an array of size n that only includes numbers in the range [1, n], sort
the array in a single pass in O(N) runtime.
0 1 2 3 4
[3, 5, 2, 1, 4]
27
Cycle Sort
Approach:
Let’s imagine the array was already sorted, what would be the relationship
between the values and the indices ?
0 1 2 3 4
[1, 2, 3, 4, 5]
28
Cycle Sort
Approach:
Index = value - 1
0 1 2 3 4
[1, 2, 3, 4, 5]
29
Cycle Sort
Approach:
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[3, 5, 2, 1, 4]
|
Where should 3 be placed at ?
30
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[3, 5, 2, 1, 4]
|
The value 3 should be placed at index 2. So we swap
31
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[2, 5, 3, 1, 4]
|
The value 2 is not in the correct place either, where should it be ?
32
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[2, 5, 3, 1, 4]
|
Swap
33
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[5, 2, 3, 1, 4]
|
Where should 5 be ?
34
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[5, 2, 3, 1, 4]
|
Swap
35
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[4, 2, 3, 1, 5]
|
Where should 4 be ?
36
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[4, 2, 3, 1, 5]
|
Swap
37
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[1, 2, 3, 4, 5]
|
Where should 1 be ?
38
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[1, 2, 3, 4, 5]
|
It is finally in its correct position, so we move our pointer
39
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[1, 2, 3, 4, 5]
|
Where should 2 be ?
40
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[1, 2, 3, 4, 5]
|
Where should 3 be ?
41
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[1, 2, 3, 4, 5]
|
Where should 4 be ?
42
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[1, 2, 3, 4, 5]
|
Where should 5 be ?
43
Cycle Sort
This means, we can use the values to know where exactly in the array they should
be placed.
0 1 2 3 4
[1, 2, 3, 4, 5]
|
Array is sorted.
44
Can you implement the function cycleSort ?
Implement Here
45
Q: What do you think is the time complexity for the aforementioned
sorting Algorithm?
?
46
Time & Space Complexity
Cycle Sort
47
Time & Space Complexity
Cycle Sort
48
Further Reading
● There are also other popular sorting algorithms such as Radix Sort, Binary
Insertion Sort…
49
Pair Programming
50
Practice Problems
Missing Number
Find All Numbers Disappeared in an Array
Find all duplicates in an array
Set Mismatch
Find the Duplicate Number
FIrst Missing Positive
Kth Largest Element in an Array
51
Resources
52
Quote of the Day
53