Skip to content

Commit c62e5f2

Browse files
committed
fix: sorting
1 parent e90d166 commit c62e5f2

2 files changed

Lines changed: 109 additions & 4 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Sorting algorithms
2+
3+
## Swap function
4+
5+
```c++
6+
void swap(int &a, int &b) {
7+
int temp = a;
8+
a = b;
9+
b = temp;
10+
}
11+
```
12+
13+
## Bubble sort
14+
15+
```c++
16+
void bubble_sort(int arr[], int n) {
17+
for (int i = 0; i < n; i++) { // n passes
18+
for (int j = 0; j < n - 1; j++) { // linear pass
19+
if (arr[j] > arr[j + 1]) { // swap if the element is greater than the next
20+
swap(arr[j], arr[j + 1]);
21+
}
22+
}
23+
}
24+
}
25+
```
26+
27+
Is it possible to optimize the bubble sort algorithm?
28+
- The example above always pass from the beginning to the end of the array, but it is possible to stop the inner loop earlier if the right side of the array is already sorted.
29+
- You can count how many swaps you did in the inner loop, and if you did 0 swaps, you can stop the outer loop.
30+
31+
### Questions:
32+
33+
- What is the best case scenario for the bubble sort?
34+
- What is the worst case scenario for the bubble sort?
35+
- How many writes does the bubble sort do?
36+
- How many reads does the bubble sort do?
37+
- What is the time complexity of the bubble sort?
38+
- What is the space complexity of the bubble sort?
39+
40+
## Selection sort
41+
42+
```c++
43+
void selection_sort(int arr[], int n) {
44+
for (int i = 0; i < n - 1; i++) { // n - 1 passes
45+
int min_index = i; // the minimum element in the unsorted part of the array
46+
for (int j = i + 1; j < n; j++) { // linear search
47+
if (arr[j] < arr[min_index]) { // find the minimum element
48+
min_index = j;
49+
}
50+
}
51+
swap(arr[i], arr[min_index]); // swap the minimum element with the first element of the unsorted part
52+
}
53+
}
54+
```
55+
56+
### Questions:
57+
58+
- What is the best case scenario for the selection sort?
59+
- What is the worst case scenario for the selection sort?
60+
- How many writes does the selection sort do?
61+
- How many reads does the selection sort do?
62+
- What is the time complexity of the selection sort?
63+
- What is the space complexity of the selection sort?
64+
- What is the difference between the bubble sort and the selection sort?
65+
66+
## Insertion sort
67+
68+
```c++
69+
void insertion_sort(int arr[], int n) {
70+
for (int i = 1; i < n; i++) { // n - 1 passes
71+
int key = arr[i]; // the key element to be inserted in the sorted part of the array
72+
int j = i - 1; // the last element of the sorted part of the array
73+
while (j >= 0 && arr[j] > key) { // shift the elements to the right to make space for the key
74+
arr[j + 1] = arr[j];
75+
j--;
76+
}
77+
arr[j + 1] = key; // insert the key in the right position
78+
}
79+
}
80+
```
81+
82+
### Questions:
83+
84+
- What is the best case scenario for the insertion sort?
85+
- What is the worst case scenario for the insertion sort?
86+
- How many writes does the insertion sort do?
87+
- How many reads does the insertion sort do?
88+
- What is the time complexity of the insertion sort?
89+
- What is the space complexity of the insertion sort?
90+
- What is the difference between the bubble sort, the selection sort, and the insertion sort?
91+
92+
## Discussion
93+
94+
- Why is sorting typically taught towards the beginning of an algorithms course?
95+
- Why do we study algorithms like bubble sort that are almost never used in practice?
96+
- Can you describe a non-comparative sorting algorithm?
97+
- Which of these sorting algorithms is the best: bubble sort, selection sort, or insertion sort?
98+
99+
Table of differences between the sorting algorithms:
100+
101+
| Algorithm | Best case | Worst case | Time complexity | Space complexity | Swaps |
102+
|-----------|-----------|------------|-----------------|------------------|-------|
103+
| Bubble | O(n) | O(n^2) | O(N^2) | O(1) | O(n^2)|
104+
| Selection | O(n^2) | O(n^2) | O(N^2) | O(1) | O(n) |
105+
| Insertion | O(n) | O(n^2) | O(N^2) | O(1) | O(n^2)|

overrides/main.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{% extends "base.html" %}
22

3-
{% block extrahead %}
3+
<!--{% block extrahead %}-->
44
<!--<meta property="og:type" content="website">-->
55
<!--<meta property="og:title" content="Awesome Gamedev Guild">-->
66
<!--<meta property="og:description" content="Game Guild">-->
7-
<!--<meta property="og:url" content="https://gameguild.gg/">-->
8-
<!--<meta property="og:image" content="https://gameguild.gg/overrides/favicon/apple-touch-icon.png">-->
9-
{% endblock %}
7+
<!--<meta property="og:url" content="https://courses.tolstenko.net/">-->
8+
<!--<meta property="og:image" content="https://courses.tolstenko.net/overrides/favicon/apple-touch-icon.png">-->
9+
<!--{% endblock %}-->
1010

1111

1212
{% block scripts %}

0 commit comments

Comments
 (0)