Skip to content

Commit c2e758a

Browse files
ElissaElissa
authored andcommitted
Update readme.md
1 parent 463418a commit c2e758a

1 file changed

Lines changed: 39 additions & 41 deletions

File tree

‎readme.md‎

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -176,60 +176,58 @@ Recursive cases *must* be written in a way that will eventually allow us to reac
176176
## Recursive Sorting Algorithms
177177

178178
### Merge Sort
179-
[overview]
179+
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+
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.
180181

181182
[(VIDEO) Merge-sort with Transylvanian-saxon (German) folk dance ![alt text](https://i.ytimg.com/vi/XaqR3G_NVoo/hqdefault.jpg)](https://www.youtube.com/watch?v=XaqR3G_NVoo)
182183

183184
#### Algorithm
184185
```
185-
TBC
186+
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")
188+
3. Start merging your single lists of one element together into larger, sorted sets
189+
4. Repeat step 3 until the entire data set has been reassembled
186190
```
187-
>CHALLENGE: One implementation is shown below, but take a stab at writing the code for this yourself before scrolling down!
188191

189192
#### Implementation in Python
190193
```
191-
def mergeSort():
192-
//TBC
193-
194-
// try it out
195-
var arr = [2,5,9,7,4,1,3,8,6];
196-
print( "Unsorted array: " + arr);
197-
arr = mergeSort( arr );
198-
print( "Sorted array: " + arr);
199-
194+
### helper function
195+
def merge( arrA, arrB ):
196+
elements = len( arrA ) + len( arrB )
197+
merged_arr = [ ]
198+
a = 0
199+
b = 0
200+
# since arrA and arrB already sorted, we only need to compare the first element of each when merging!
201+
for i in range( 0, elements ):
202+
if a >= len(arrA): # all elements in arrA have been merged
203+
merged_arr.append(arrB[b])
204+
b += 1
205+
elif b >= len(arrB): # all elements in arrB have been merged
206+
merged_arr.append(arrA[a])
207+
a += 1
208+
elif arrA[a] < arrB[b]: # next element in arrA smaller, so add to final array
209+
merged_arr.append(arrA[a])
210+
a += 1
211+
else: # else, next element in arrB must be smaller, so add it to final array
212+
merged_arr.append(arrB[b])
213+
b += 1
214+
return merged_arr
215+
216+
217+
### recursive sorting function
218+
def merge_sort( arr ):
219+
if len( arr ) > 1:
220+
left = merge_sort( arr[ 0 : len( arr ) / 2 ] )
221+
right = merge_sort( arr[ len( arr ) / 2 : ] )
222+
arr = merge( left, right ) # merge() defined later
223+
return arr
200224
```
201225

202226
#### Real-World Applications
203-
Have you ever wondered how some of the languages you use actually implement their built-in `sort()` functions? Many of them actually utilize the ***Merge Sort*** algorithm!
227+
Have you ever wondered how some of the languages you use actually implement their built-in `sort()` functions? Many of them actually utilize the ***Merge Sort*** algorithm! *WHY* they do so is because this sorting algorithm is reliably efficient. In all cases, regardless of how sorted the original data set might be, this algorithm will have a runtime of O(n log(n)), one of the better sorting runtimes out there.
204228

205-
#### Check for understanding
206-
1. TBD
207-
<details><summary>Answer</summary> ... </details>
208-
209-
2. When using ***Merge Sort***, what is the difference between the order or arrangement of elements in *best case* versus *worst case*?
210-
<details><summary>Answer</summary> ... </details>
211-
212-
3. When using ***Merge Sort***, what would be the runtime of sorting elements in the best, average, and worst cases?
213-
<details><summary>Answer</summary>
214-
<ul><li>Best case: O(nlog(n))</li>
215-
<li>Average case: O(nlog(n))</li>
216-
<li>Worst case: O(nlog(n))</li>
217-
</details>
218-
219-
4. Show how the array **[4, 8, 3, 1, 9, 6]** changes as it is being sorted using ***Merge 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_)
220-
<details><summary>Answer</summary> <pre><samp> [4, 8, 3, 1, 9, 6] // divide
221-
/ \
222-
[4, 8, 3] [1, 9, 6]
223-
/ \ / \
224-
[4, 8] [3] [1, 9] [6]
225-
/ \ | / \ |
226-
[4] [8] [3] [1] [9] [6] // merge
227-
\ / | \ / |
228-
[4, 8] [3] [1, 9] [6]
229-
\ / \ /
230-
[3, 4, 8] [1, 6, 9]
231-
\ /
232-
[1, 3, 4, 6, 8, 9] </samp></pre></details>
229+
#### Your Task
230+
- (STRETCH) Try writing an *in-place* ***Merge Sort*** algorithm.
233231

234232
### Quick Sort
235233
[overview]

0 commit comments

Comments
 (0)