

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Sorting Algorithms
incomplete
2: Bubble Sort
incomplete
3: Bubble Sort Big O
incomplete
4: Why Bubble Sort?
incomplete
5: Merge Sort
incomplete
6: Merge Sort Big O
incomplete
7: Why Merge Sort?
incomplete
8: Insertion Sort
incomplete
9: Insertion Sort Big O
incomplete
10: Why Use Insertion Sort?
incomplete
11: Quick Sort
incomplete
12: Quick Sort Big O
incomplete
13: Fixing Quick Sort
incomplete
14: Why Use Quick Sort?
incomplete
15: Selection Sort
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Pros:
O(n*log(n)) instead of O(n^2).Cons:
def merge_sort(nums: list[int]) -> list[int]:
if len(nums) < 2:
return nums
first = merge_sort(nums[: len(nums) // 2])
second = merge_sort(nums[len(nums) // 2 :])
return merge(first, second)
def merge(first: list[int], second: list[int]) -> list[int]:
final = []
i = 0
j = 0
while i < len(first) and j < len(second):
if first[i] <= second[j]:
final.append(first[i])
i += 1
else:
final.append(second[j])
j += 1
while i < len(first):
final.append(first[i])
i += 1
while j < len(second):
final.append(second[j])
j += 1
return final