feat: add lectures for searching and sorting algorithms (#61663)

Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com>
Co-authored-by: Sem Bauke <sem@freecodecamp.org>
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com>
This commit is contained in:
Ilenia
2025-08-06 18:07:47 +02:00
committed by GitHub
parent 0e81a59d30
commit 2a577ade98
5 changed files with 311 additions and 47 deletions

View File

@@ -4417,7 +4417,8 @@
"lecture-searching-and-sorting-algorithms": {
"title": "Searching and Sorting Algorithms",
"intro": [
"Learn about Searching and Sorting Algorithms in these lectures."
"Learn about fundamental searching and sorting algorithms, including linear search, binary search, and merge sort.",
"These lectures cover algorithm implementations, time and space complexity analysis, and the divide and conquer programming paradigm."
]
},
"workshop-binary-search": {

View File

@@ -6,4 +6,4 @@ superBlock: full-stack-developer
## Introduction to Searching and Sorting Algorithms
Learn about Searching and Sorting Algorithms in these lectures.
Learn about fundamental searching and sorting algorithms, including linear search, binary search, and merge sort. These lectures cover algorithm implementations, time and space complexity analysis, and the divide and conquer programming paradigm.

View File

@@ -5,6 +5,15 @@
"blockType": "lecture",
"blockLayout": "challenge-list",
"superBlock": "full-stack-developer",
"challengeOrder": [{ "id": "68420c70e4bfe26d52b780dc", "title": "Step 1" }],
"challengeOrder": [
{
"id": "68420c70e4bfe26d52b780dc",
"title": "What Is Binary Search and How Does It Differ From Linear Search?"
},
{
"id": "68910fc037a90c285107af04",
"title": "What Is Divide and Conquer, and How Does Merge Sort Work?"
}
],
"helpCategory": "Python"
}

View File

@@ -1,135 +1,189 @@
---
id: 68420c70e4bfe26d52b780dc
# title needs to be updated to correct title when lectures are finalized
title: Searching and Sorting Algorithms
title: What Is Binary Search and How Does It Differ From Linear Search?
challengeType: 19
# dashedName needs to be updated to correct title when lectures are finalized
dashedName: lecture-searching-and-sorting-algorithms
dashedName: what-is-binary-search-and-how-does-it-differ-from-linear-search
---
# --description--
Watch the video or read the transcript and answer the questions below.
Searching through a list of items is a common occurrence in computer science. There are two key algorithms you should know about when it comes to searching: linear search and binary search.
Linear search starts at the beginning of a list and iterates through each item until it finds the target value it is looking for.
If the target value is found, the index where it's located in the list is returned. If the target value isn't found, `-1` is returned. We return `-1` because it's not a valid index in most programming languages.
Here is what the code looks like for linear search:
```python
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
```
If the list we'll search through is `[13, 4, 7, 9, 10]` and the target value is `9`, the function would return `3` because `9` is at index `3`.
If we changed the target value to `5`, the function would return `-1` because `5` is not in the list.
While this is a relatively straightforward algorithm, it is not the most efficient. If you have a large list of items, linear search can take a long time to find the target value.
The time complexity of linear search is `O(n)` because the time it takes to search through the list grows linearly with the size of the list.
The space complexity of linear search is `O(1)` because it doesn't require any additional space to search through the list.
Binary search is a more efficient algorithm for searching through a large list of items. The condition here is that the list must be sorted in ascending order.
Binary search works by dividing the list in half and checking if the target value is in the middle of the list. If the target value is in the middle of the list, the index of the target value is returned. Otherwise, the algorithm checks if the target value is in the left or right half of the list.
It continues to divide the remaining parts of the list into halves until the target value is found. If the target value is not in the list, it returns `-1`
Here is what the code looks like for binary search:
```python
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
```
We start by identifying a `low` and `high` index. This represents the range of the list we are searching through.
We then check the condition of `low` being less than or equal to `high`. If `low` is greater than `high`, we have searched through the entire list and the target value is not found. In that case we stop the search and return `-1`.
If the `low` index is less than or equal to the `high` index, we calculate the middle index of the list, `mid`. We then check if the target value is at the middle index. If it is, we return the middle index.
Otherwise, we check if the value at the midpoint is less than the target. If it is, we update the low index to be the middle index plus one. This means we will search the right half of the list.
Lastly, if none of the other conditions are `True`, we update the `high` index to be the middle index minus one. This means we will search the left half of the list.
We continue to repeat this process until we find the target or determine that the target is not in the list.
The time complexity of binary search is `O(log n)` because the time it takes to search through the list grows logarithmically with the size of the list.
The space complexity of binary search is `O(1)` because it doesn't require any additional space to search through the list.
Binary search and linear search can be used for a variety of problems you will encounter in computer science. It is important to understand the differences between the two algorithms and when to use each one.
# --questions--
## --text--
Question 1
What is the main difference between linear search and binary search?
## --answers--
Answer 1.1
Linear search is faster than binary search.
### --feedback--
Feedback 1
Consider the requirements and efficiency of each algorithm.
---
Answer 1.2
### --feedback--
Feedback 1
Binary search requires a sorted list, while linear search does not.
---
Answer 1.3
Linear search can only be used with numbers.
### --feedback--
Feedback 1
Consider the requirements and efficiency of each algorithm.
---
Answer 1.4
Binary search always returns the first occurrence of a target.
### --feedback--
Feedback 1
Consider the requirements and efficiency of each algorithm.
## --video-solution--
5
2
## --text--
Question 2
What is the time complexity of linear search?
## --answers--
Answer 2.1
`O(1)`
### --feedback--
Feedback 2
Think about how the time to complete the search changes with the size of the list.
---
Answer 2.2
`O(log n)`
### --feedback--
Feedback 2
Think about how the time to complete the search changes with the size of the list.
---
Answer 2.3
### --feedback--
Feedback 2
`O(n)`
---
Answer 2.4
`O(n²)`
### --feedback--
Feedback 2
Think about how the time to complete the search changes with the size of the list.
## --video-solution--
5
3
## --text--
Question 3
In binary search, what happens if the target value is not found in the list?
## --answers--
Answer 3.1
It returns the middle index.
### --feedback--
Feedback 3
Reflect on what the function is designed to do when the target is absent.
---
Answer 3.2
### --feedback--
Feedback 3
It returns `-1`.
---
Answer 3.3
It returns the last index checked.
### --feedback--
Feedback 3
Reflect on what the function is designed to do when the target is absent.
---
Answer 3.4
It enters an infinite loop.
### --feedback--
Feedback 3
Reflect on what the function is designed to do when the target is absent.
## --video-solution--
5
2

View File

@@ -0,0 +1,200 @@
---
id: 68910fc037a90c285107af04
title: What Is Divide and Conquer, and How Does Merge Sort Work?
challengeType: 19
dashedName: what-is-divide-and-conquer-and-how-does-merge-sort-work
---
# --description--
The divide and conquer paradigm in computer science is a technique for recursively breaking down problems into smaller sub problems. One of the key aspects of this technique is recursion, which happens when a function calls itself repeatedly until a base case is reached. In this lecture, we will take a look at the merge sort algorithm to better understand how the divide and conquer technique works.
Let's say we had this list of numbers:
```md
42 37 53 17
```
The goal is to sort that list from smallest to largest using the merge sort algorithm. The first step is to divide that list in half:
```md
42 37 | 53 17
```
Then we need to look at the left side of the list:
```md
42 37
```
We take that sub list and divide in half again until each sub list has only one item in it:
```md
42 | 37
```
A list with only one item in it is sorted by default. Next we need to merge each of those one element sub lists into a sorted list:
```md
37 42
```
Then we follow the same process for the right side of the original list:
```py
# right side of original list
53 17
# divide the list in half
53 | 17
# merge the lists in sorted order
17 53
```
Now that both halves of the original list are sorted, we merge those two halves together and sort the elements:
```markdown
17 37 42 53
```
Here is what the algorithm looks like in code:
```py
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
sorted_list = []
i = 0
j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
sorted_list.append(left[i])
i += 1
else:
sorted_list.append(right[j])
j += 1
sorted_list.extend(left[i:])
sorted_list.extend(right[j:])
return sorted_list
```
The time complexity for merge sort would be `O(n log n)` because the list is continuously divided in half (`log n`) and then merged together (`O(n)`). Unlike other sorting algorithms like bubble sort, merge sort is not sorted in place and has a space complexity of `O(n)`.
# --questions--
## --text--
What is the divide and conquer paradigm in computer science?
## --answers--
A technique for detecting a cycle in function value iterations using just two iterators.
### --feedback--
Review the beginning of the lecture.
---
An algorithm for comparing two elements and swapping them from smallest to largest if needed.
### --feedback--
Review the beginning of the lecture.
---
A technique for recursively breaking down problems into smaller sub problems.
---
An algorithm to compute the shortest connecting network for points in a plane.
### --feedback--
Review the beginning of the lecture.
## --video-solution--
3
## --text--
What is the time complexity for the merge sort algorithm?
## --answers--
`O(n log n)`
---
`O(log n²)`
### --feedback--
Review the end of the lecture.
---
`O(n³ log n)`
### --feedback--
Review the end of the lecture.
---
`O(log n³)`
### --feedback--
Review the end of the lecture.
## --video-solution--
1
## --text--
What is the space complexity for the merge sort algorithm?
## --answers--
`O(n²)`
### --feedback--
Review the end of the lecture.
---
`O(1)`
### --feedback--
Review the end of the lecture.
---
`O(n log n)`
### --feedback--
Review the end of the lecture.
---
`O(n)`
## --video-solution--
4