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 b259b8b
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: readme.md
+21-12Lines changed: 21 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
# Sorting Algorithms
2
-
For the purpose of this module, we will be writing and discussing all sorting algorithms with the assumption that our goal is to sort items in ascending order. But be aware, this does not always have to be the case.
2
+
*For the purpose of this module, we will be writing and discussing all sorting algorithms with the assumption that our goal is to sort items in ascending order. But be aware, this does not always have to be the case.*
3
3
4
4
## Day 1 - Why is it so important to sort data?
5
5
@@ -31,7 +31,7 @@ A visualization comparing these two algorithms is shown below.
31
31

32
32
33
33
#### Your Task
34
-
- (STRETCH) Complete some of all of the three functions in `searching.py`
34
+
- (STRETCH) Complete some or all of the three functions in `searching.py`
35
35
36
36
#### Runtimes
37
37
@@ -50,12 +50,13 @@ While the justification for *WHY* we want to sort our data is pretty clear, a ha
50
50
## Iterative Sorting Algorithms
51
51
52
52
### Selection Sort
53
-
***Selection Sort*** is an algorithm that many of you have probably used before when sorting things in your everyday lives. Imagine that you have several books you want to arrange on a shelf from shortest to tallest. You start off by looking for the shortest book, and when you find it, put it on the far left side of the shelf. Then, you look for the second shortest book and insert it directly to the right of the first book. You repeat this process, selecting the next shortest book and moving it next to the most recently placed book, until you have moved all books into the right place. This is ***Selection Sort***.
53
+
***Selection Sort*** is an algorithm that many of you have probably used before when sorting things in your everyday lives. Imagine that you have several books you want to arrange on a shelf from shortest to tallest. You start off by looking for the shortest book, and when you find it, put it on the far left side of the shelf. Then, you look for the second shortest book and insert it directly to the right of the first book. You repeat this process, selecting the next shortest book and moving it next to the most recently placed book, until you have moved all books into the correct place. This is ***Selection Sort***.
54
54
55
55
An example of this algorithm being applied to an array with 10 numerical elements can be seen in the video below.
56
56
57
-
[(VIDEO) Select-sort with Gypsy folk dance](https://www.youtube.com/watch?v=Ns4TPTC8whw)
57
+
[(VIDEO) Select-sort with Gypsy folk dance](https://www.youtube.com/watch?v=Ns4TPTC8whw)
58
58
59
+
[](https://www.youtube.com/watch?v=Ns4TPTC8whw)
59
60
60
61
#### Algorithm
61
62
1. Start with current index = 0
@@ -107,7 +108,9 @@ Recall that the runtime complexity of an algorithm, often expressed using *Big O
107
108
### Insertion Sort
108
109
Think back to class or team picture day. Everyone stands in a line facing the photographer. Starting at the left-hand side of the line, the photographer checks to make sure each person is taller than the person next to them. If they are shorter, the photographer pulls them out and shifts people over to the right until he or she finds the right spot for this person. They then insert them back into the line. This process repeats until the photographer reaches the last person on the right-hand side, who must be the tallest person in the group. This is ***Insertion Sort***.
109
110
110
-
[(VIDEO) Insert-sort with Romanian folk dance ](https://www.youtube.com/watch?v=ROalU379l3U)
111
+
[(VIDEO) Insert-sort with Romanian folk dance](https://www.youtube.com/watch?v=ROalU379l3U)
112
+
113
+
[](https://www.youtube.com/watch?v=ROalU379l3U)
111
114
112
115
#### Algorithm
113
116
1. Separate the first element from the rest of the array. Think about it as a sorted list of one element.
@@ -139,7 +142,7 @@ print(arr);
139
142
#### Real-World Applications
140
143
The answer to the question, "Is ***Insertion Sort*** an efficient algorithm?" is not always the same. The runtime of ***Insertion Sort*** is dependent on how close to being "in-order" the data is to begin with. In a scenario where you are performing ***Insertion Sort*** on an already or mostly sorted array, very few elements will need to be shifted over, leading to a runtime of Ω(n). However, in a worse-case scenario, where the maximum number of shifts are being performed, the runtime of this algorithm is O(n²).
141
144
142
-
### MVP
145
+
### TO-DO in iterative_sorting.py
143
146
- Complete the missing parts of `selection_sort()`
144
147
- Implement `insertion_sort()` in `iterative_sorting.py`
145
148
@@ -179,12 +182,16 @@ Recursive cases *must* be written in a way that will eventually allow us to reac
179
182
Your boss asks you to organize an old filing cabinet with 20 years worth of financial documents. He would like things ordered chronologically. You feel overwhelemed. There are thousands of papers in this thing.
180
183
So you decide to break this insane task up into more manageable pieces. First, you focus on organizing a single drawer. But this still has a lot of pieces of data that need to be sorted. Within the drawer, you pull out a single folder. You lay out all the contents on a table, grabbing two pieces at a time, placing the older document on top of the newer one. Then, you start merging sorted sets of documents together until the folder is done. Once all the folders have been reassembled, you order the folders correctly in the drawer. And then you put sorted drawers back into the filing cabinet. This idea of breaking a large set of data down into small pieces, sorting the pieces, then merging them back together is what ***Merge Sort*** is all about.
181
184
182
-
[(VIDEO) Merge-sort with Transylvanian-saxon (German) folk dance ](https://www.youtube.com/watch?v=XaqR3G_NVoo)
185
+
[(VIDEO) Merge-sort with Transylvanian-saxon (German) folk dance](https://www.youtube.com/watch?v=XaqR3G_NVoo)
186
+
187
+
[](https://www.youtube.com/watch?v=XaqR3G_NVoo)
188
+
183
189
184
190
#### Algorithm
185
191
```
186
192
1. While your data set contains more than one item, split it in half
187
-
2. Once you have gotten down to a single element, you have also *sorted* that element (a single element cannot be "out of order")
193
+
2. Once you have gotten down to a single element, you have also *sorted* that element
194
+
(a single element cannot be "out of order")
188
195
3. Start merging your single lists of one element together into larger, sorted sets
189
196
4. Repeat step 3 until the entire data set has been reassembled
190
197
```
@@ -232,7 +239,9 @@ Have you ever wondered how some of the languages you use actually implement thei
232
239
### Quick Sort
233
240
Let's think about the group photo example again. Everyone's lined up and the photographer wants to order individuals from shortest to tallest. They pull out the first person from the line and instruct everyone *shorter* than this person to position themselves on the left-hand side. Everyone *taller* than this person is instructed to move to the right-hand side. The photographer then repeats this process with the group of people on the left and the group of people on the right. This is ***Quick Sort***.
234
241
235
-
[(VIDEO) Quick-sort with Hungarian folk dance ](https://www.youtube.com/watch?v=ywWBy6J5gz8)
242
+
[(VIDEO) Quick-sort with Hungarian folk dance](https://www.youtube.com/watch?v=ywWBy6J5gz8)
243
+
244
+
[](https://www.youtube.com/watch?v=ywWBy6J5gz8)
236
245
237
246
238
247
#### Algorithm
@@ -253,8 +262,8 @@ def quick_sort(arr):
253
262
#### Real-World Applications
254
263
While ***Quick Sort*** has "quick" in its name, it is typically not used as frequently as Merge Sort. Although it *is* quick in a best case scenario, worst case for ***Quick Sort*** is *very* bad. Because of this, it is not often chosen for production.
255
264
256
-
### MVP
257
-
--Implement `quick_sort()` in `recursive_sorting.py`
265
+
### TO-DO in recursive_sorting.py
266
+
- Implement `quick_sort()` in `recursive_sorting.py`
258
267
259
268
### Stretch Goals
260
269
@@ -264,4 +273,4 @@ While ***Quick Sort*** has "quick" in its name, it is typically not used as freq
264
273
#### Timsort is a combination of the Merge Sort and Insertion Sort algorithms.
265
274
- What programming languages use **Timsort** to implement their built-in `sort()` functions?
266
275
- If an interviewer asked you to describe the **Tim Sort** algorithm in 3-4 sentences, what would you say?
0 commit comments