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
Copy file name to clipboardExpand all lines: Mergesort/README.markdown
+18-6Lines changed: 18 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,16 +10,28 @@ is to **divide and conquer**; To divide a big problem into smaller chunks and so
10
10
- Keep splitting the resulting piles until you can't anymore; In the end, you will have *n* piles with 1 number in each pile
11
11
- Begin to **merge** the piles together by sequentially pairing a pile with another pile. During each merge, you want to sort the contents in order
12
12
13
-
###An example
13
+
## An example
14
14
15
-
Let's say the numbers to sort are `[1, 7, 4 , 5, 9]`. This is your unsorted pile. Our goal is to keep splitting the pile until you can't anymore.
15
+
### Splitting
16
16
17
-
Split the array into two halves - `[1, 7,]` and `[4, 5, 9]`. Can you keep splitting them? Yes you can!
17
+
Let's say the numbers to sort are `[2, 1, 5 , 4, 9]`. This is your unsorted pile. Our goal is to keep splitting the pile until you can't anymore.
18
18
19
-
Focus on the left pile. `[1, 7]` will split into `[1]` and `[7]`. Can you keep splitting them? No. Time to check the other pile.
19
+
Split the array into two halves - `[2, 1,]` and `[5, 4, 9]`. Can you keep splitting them? Yes you can!
20
20
21
-
`[4, 5, 9]` splits to `[4]` and `[5, 9]`. Unsurprisingly, `[4]` can't split into anymore, but `[5, 9]` splits into `[5]` and `[9]`.
21
+
Focus on the left pile. `[2, 1]` will split into `[2]` and `[1]`. Can you keep splitting them? No. Time to check the other pile.
22
+
23
+
`[5, 4, 9]` splits to `[5]` and `[4, 9]`. Unsurprisingly, `[5]` can't split into anymore, but `[4, 9]` splits into `[4]` and `[9]`.
22
24
23
25
The splitting process ends with the following piles:
24
26
25
-
`[1]``[7]``[4]``[5]``[9]`
27
+
`[2]``[1]``[5]``[4]``[9]`
28
+
29
+
### Merging
30
+
31
+
Now that you've split the array, you'll **merge** the piles together **while sorting them**. Remember, the idea is to solve many small problems rather than a big one. For each merge iteration you'll only be concerned at merging one pile with another.
32
+
33
+
Given `[2]``[1]``[5]``[4]``[9]`, the first pass will result in `[1, 2]` and `[4, 5]` and `[9]`. Since `[9]` is the odd one out, you can't merge it with anything during this pass.
34
+
35
+
The next pass will merge `[1, 2]` and `[4, 5]` together. This results in `[1, 2, 4, 5]`, with the `[9]` left out again since it's the odd one out.
36
+
37
+
Since you're left with 2 piles, `[9]` finally gets it's chance to merge, resulting in the sorted array `[1, 2, 4, 5, 9]`.
0 commit comments