Sorting II - Part II (With Code)
Sorting II - Part II (With 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
Implementation
def partition(nums, left, right) -> int:
"""
Picks the first element left as a pivot
and returns the index of pivot value in the sorted array
"""
pivot_val = nums[left]
store_index = left + 1
for j in range(store_index, right + 1):
if nums[j] < pivot_val:
nums[store_index], nums[j] = nums[j], nums[store_index]
store_index += 1
18
Implementation
def quick_sort(nums, left, right):
# if length of array is less than or equal to 1
if left >= right:
return
19
Q: What do you think is the time complexity for the aforementioned
sorting Algorithm?
?
20
Time & Space Complexity
21
Q: what kind of input would result in the worst case?
?
22
What happens in the worst case?
Sorted
array [1, 2, 3, 4, 5]
[2] [3, 4, 5]
[3] [4, 5]
[4] [5]
23
Q: Can we do better ? How ?
?
24
How can we avoid the worst case?
25
Modified Implementation
def partition(nums, left, right) -> int:
# Select a random pivot_index and move the pivot in to the first
element
pivot_index = random.randint(left, right)
nums[pivot_index], nums[left] = nums[left], nums[pivot_index]
pivot_val = nums[left]
store_index = left + 1
for j in range(store_index, right + 1):
if nums[j] < pivot_val:
nums[store_index], nums[j] = nums[j], nums[store_index]
store_index += 1
26
Time & Space Complexity
27
Cycle/ Cyclic Sort
28
Cycle Sort
It is known that all comparison-based sorting algorithms have a lower bound time
complexity of Ω(N log N).
29
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]
30
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]
31
Cycle Sort
Approach:
Index = value - 1
0 1 2 3 4
[1, 2, 3, 4, 5]
32
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 ?
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
[3, 5, 2, 1, 4]
|
The value 3 should be placed at index 2. So we swap
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
[2, 5, 3, 1, 4]
|
The value 2 is not in the correct place either, where should it be ?
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
[2, 5, 3, 1, 4]
|
Swap
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
[5, 2, 3, 1, 4]
|
Where should 5 be ?
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
[5, 2, 3, 1, 4]
|
Swap
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
[4, 2, 3, 1, 5]
|
Where should 4 be ?
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
[4, 2, 3, 1, 5]
|
Swap
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 1 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]
|
It is finally in its correct position, so we move our pointer
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 2 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]
|
Where should 3 be ?
44
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 ?
45
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 ?
46
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.
47
Can you implement the function cycleSort ?
Implement Here
48
Cycle Sort
Implementation
def cycleSort(arr):
n = len(arr)
i = 0
while i < n:
correct_position = arr[i] - 1
if correct_position != i:
arr[correct_position], arr[i] = arr[i], arr[correct_position]
else:
i += 1
return arr
49
Q: What do you think is the time complexity for the aforementioned
sorting Algorithm?
?
50
Time & Space Complexity
Cycle Sort
51
Time & Space Complexity
Cycle Sort
52
Further Reading
● There are also other popular sorting algorithms such as Radix Sort, Binary
Insertion Sort…
53
Pair Programming
54
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
55
Resources
56
Quote of the Day
57