Skip to content

Commit 63a19d5

Browse files
committed
example complete
1 parent beea320 commit 63a19d5

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

Mergesort/README.markdown

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,28 @@ is to **divide and conquer**; To divide a big problem into smaller chunks and so
1010
- Keep splitting the resulting piles until you can't anymore; In the end, you will have *n* piles with 1 number in each pile
1111
- 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
1212

13-
### An example
13+
## An example
1414

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
1616

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.
1818

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!
2020

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]`.
2224

2325
The splitting process ends with the following piles:
2426

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

Comments
 (0)