You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b17fd93
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: Sorting.md
+14-10Lines changed: 14 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,9 +21,11 @@ The process of performing a binary search has a couple of extra steps. First, th
21
21
22
22
1. Compare the item in the middle of the data set to the item we are searching for.
23
23
- If it is the same, stop. We found it and are done!
24
+
- Else, if the middle was the last item to search, return that the item was NOT found
24
25
- Else, if the item we are searching for is LESS than the item in the middle:
25
-
- Eliminate the RHS of list. Repeat step 1 with only the LHS of list.
26
-
- Else: ...
26
+
- Eliminate the `RHS` of list. Repeat step 1 with only the `LHS` of list.
27
+
- Else, the item we are searching for is GREATER than the item in the middle:
28
+
- Eliminate the `LHS` of list. Repeat step 1 with only `RHS` of list.
27
29
28
30
29
31
A visualization comparing these two algorithms is shown below.
@@ -99,36 +101,35 @@ While ***Selection Sort*** is one of the easier sorting algorithms to understand
99
101
Recall that the runtime complexity of an algorithm, often expressed using *Big O notation*, tells us how the amount of operations our algorithm requires will grow as the size of our input grows. ***Selection Sort*** will have a runtime of O(n²) in *all* cases, making it impractical to use with many large, real-world data sets.
100
102
101
103
#### Check for understanding
102
-
1. Is ***Selection Sort*** a comparison sorting algorithm?
103
-
<details><summary>Answer</summary> Yes, since to <i>select</i> the desired element, we compare a current value to the rest of the "unsorted" segment of our list or array.
104
-
</details>
105
104
106
-
2. Why do we end our loop before processing the item in the last index of an array when performing ***Selection Sort***?
105
+
1. Why do we end our loop before processing the item in the last index of an array when performing ***Selection Sort***?
107
106
<details><summary>Answer</summary> By the nature of how this algorithm works, if all other elements have been sorted, the last element will have been moved to the correct index.
108
107
109
-
3. When using ***Selection Sort***, what is the difference between the order or arrangement of elements in *best case* versus *worst case*?
108
+
2. When using ***Selection Sort***, what is the difference between the order or arrangement of elements in *best case* versus *worst case*?
110
109
<details><summary>Answer</summary> None, since the same number of operations will be performed <b>regardless</b> of how elements are ordered.
111
110
</details>
112
111
113
-
4. When using ***Selection Sort***, what would be the runtime of sorting elements in the best, average, and worst cases?
112
+
3. When using ***Selection Sort***, what would be the runtime of sorting elements in the best, average, and worst cases?
114
113
<details><summary>Answer</summary> <ul>
115
114
<li>Best case: O(n²)</li>
116
115
<li>Average case: O(n²)</li>
117
116
<li>Worst case: O(n²)</li>
118
117
</ul>
119
118
</details>
120
-
5. Show how the array **[4, 8, 3, 1, 9, 6]** changes as it is being sorted using ***Selection Sort***. To do this, write out the contents of the array after each pass of the algorithm. (_hint...we should only need n-1 passes to sort the array, where n is the size_)
119
+
4. Show how the array **[4, 8, 3, 1, 9, 6]** changes as it is being sorted using ***Selection Sort***. To do this, write out the contents of the array after each pass of the algorithm. (_hint...we should only need n-1 passes to sort the array, where n is the size_)
***Insertion Sort***, like Selection Sort is an iterative sorting algorithm. However, it improves slightly on the performance of Selection Sort. Imagine you lay out several playing cards in a line on a table. Consider the first card to be a *sorted* subarray of lengh = 1. Pick up each card (in this case, from left to right) and insert it into the correct position in the *sorted* subarray. Shift other cards to the right to make room for the card you are inserting as you go.
129
128
130
129
[(VIDEO) Insert-sort with Romanian folk dance ](https://www.youtube.com/watch?v=ROalU379l3U)
131
130
131
+
The manner in which we process elements when performing ***Insertion Sort*** means that, depending on the original order of elements, on each pass of the array, we may only have to do 1 comparison to determine if an element is in the right position. However, in a worst-case scenario, we will perform just as many comparisons as with Selection Sort, meaning the runtime of this algorithm is also O(n²).
132
+
132
133
#### Algorithm
133
134
1. Separate the first element from the rest of the array. Consider it a sorted list of one element.
134
135
@@ -222,6 +223,8 @@ Recursive cases *must* be written in a way that will eventually allow us to reac
222
223
223
224
[(VIDEO) Merge-sort with Transylvanian-saxon (German) folk dance ](https://www.youtube.com/watch?v=XaqR3G_NVoo)
224
225
226
+
[runtime]
227
+
225
228
#### Algorithm
226
229
```
227
230
TBC
@@ -278,6 +281,7 @@ Have you ever wondered how some of the languages you use actually implement thei
278
281
279
282
[(VIDEO) Quick-sort with Hungarian folk dance ](https://www.youtube.com/watch?v=ywWBy6J5gz8)
0 commit comments