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: Merge Sort/README.markdown
+19-19
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Goal: Sort an array from low to high (or high to low)
6
6
7
7
Invented in 1945 by John von Neumann, merge-sort is an efficient algorithm with a best, worst, and average time complexity of **O(n log n)**.
8
8
9
-
The merge-sort algorithm uses the **divide and conquer** approach which is to divide a big problem into smaller problems and solve them. I think of the merge-sort algorithm as **split first** and **merge after**.
9
+
The merge-sort algorithm uses the **divide and conquer** approach which is to divide a big problem into smaller problems and solve them. I think of the merge-sort algorithm as **split first** and **merge after**.
10
10
11
11
Assume you need to sort an array of *n* numbers in the right order. The merge-sort algorithm works as follows:
12
12
@@ -19,25 +19,25 @@ Assume you need to sort an array of *n* numbers in the right order. The merge-so
19
19
20
20
### Splitting
21
21
22
-
Assume you are given an array of *n* numbers as`[2, 1, 5, 4, 9]`. This is an unsorted pile. The goal is to keep splitting the pile until you cannot split anymore.
22
+
Assume you are given an array of *n* numbers as`[2, 1, 5, 4, 9]`. This is an unsorted pile. The goal is to keep splitting the pile until you cannot split anymore.
23
23
24
24
First, split the array into two halves: `[2, 1]` and `[5, 4, 9]`. Can you keep splitting them? Yes, you can!
25
25
26
26
Focus on the left pile. Split`[2, 1]` into `[2]` and `[1]`. Can you keep splitting them? No. Time to check the other pile.
27
27
28
-
Split `[5, 4, 9]` into `[5]` and `[4, 9]`. Unsurprisingly, `[5]` cannot be split anymore, but `[4, 9]` can be split into `[4]` and `[9]`.
28
+
Split `[5, 4, 9]` into `[5]` and `[4, 9]`. Unsurprisingly, `[5]` cannot be split anymore, but `[4, 9]` can be split into `[4]` and `[9]`.
29
29
30
30
The splitting process ends with the following piles: `[2]``[1]``[5]``[4]``[9]`. Notice that each pile consists of just one element.
31
31
32
32
### Merging
33
33
34
34
Now that you have split the array, you should **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 must be concerned at merging one pile with another.
35
35
36
-
Given the piles `[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 cannot merge it with anything during this pass.
36
+
Given the piles `[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 cannot merge it with anything during this pass.
37
37
38
-
The next pass will merge `[1, 2]` and `[4, 5]` together. This results in `[1, 2, 4, 5]`, with the `[9]` left out again because it is the odd one out.
38
+
The next pass will merge `[1, 2]` and `[4, 5]` together. This results in `[1, 2, 4, 5]`, with the `[9]` left out again because it is the odd one out.
39
39
40
-
You are left with only two piles and `[9]`, finally gets its chance to merge, resulting in the sorted array as `[1, 2, 4, 5, 9]`.
40
+
You are left with only two piles `[1, 2, 4, 5]`and `[9]`, finally gets its chance to merge, resulting in the sorted array as `[1, 2, 4, 5, 9]`.
41
41
42
42
## Top-down implementation
43
43
@@ -61,7 +61,7 @@ A step-by-step explanation of how the code works:
61
61
62
62
1. If the array is empty or contains a single element, there is no way to split it into smaller pieces. You must just return the array.
63
63
64
-
2. Find the middle index.
64
+
2. Find the middle index.
65
65
66
66
3. Using the middle index from the previous step, recursively split the left side of the array.
@@ -144,16 +144,16 @@ This process repeats. At each step, we pick the smallest item from either the `l
144
144
leftPile rightPile orderedPile
145
145
[ 1, 7, 8 ] [ 3, 6, 9 ] [ 1, 3, 6 ]
146
146
l -->r
147
-
147
+
148
148
leftPile rightPile orderedPile
149
149
[ 1, 7, 8 ] [ 3, 6, 9 ] [ 1, 3, 6, 7 ]
150
150
-->l r
151
-
151
+
152
152
leftPile rightPile orderedPile
153
153
[ 1, 7, 8 ] [ 3, 6, 9 ] [ 1, 3, 6, 7, 8 ]
154
154
-->l r
155
155
156
-
Now, there are no more items in the left pile. We simply add the remaining items from the right pile, and we are done. The merged pile is `[ 1, 3, 6, 7, 8, 9 ]`.
156
+
Now, there are no more items in the left pile. We simply add the remaining items from the right pile, and we are done. The merged pile is `[ 1, 3, 6, 7, 8, 9 ]`.
157
157
158
158
Notice that, this algorithm is very simple: it moves from left-to-right through the two piles and at every step picks the smallest item. This works because we guarantee that each of the piles is already sorted.
The speed of the merge-sort algorithm is dependent on the size of the array it needs to sort. The larger the array, the more work it needs to do.
242
+
The speed of the merge-sort algorithm is dependent on the size of the array it needs to sort. The larger the array, the more work it needs to do.
243
243
244
-
Whether or not the initial array is sorted already doesnot affect the speed of the merge-sort algorithm since you will be doing the same amount splits and comparisons regardless of the initial order of the elements.
244
+
Whether or not the initial array is sorted already does not affect the speed of the merge-sort algorithm since you will be doing the same amount splits and comparisons regardless of the initial order of the elements.
245
245
246
-
Therefore, the time complexity for the best, worst, and average case will always be **O(n log n)**.
246
+
Therefore, the time complexity for the best, worst, and average case will always be **O(n log n)**.
247
247
248
248
A disadvantage of the merge-sort algorithm is that it needs a temporary "working" array equal in size to the array being sorted. It is not an **in-place** sort, unlike for example [quicksort](../Quicksort/).
Copy file name to clipboardExpand all lines: Selection Sort/README.markdown
+2-2
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ It works as follows:
19
19
- Find the lowest number in the rest of the array, starting from index 2, and swap it with the one at index 2. Now, the array is sorted from index 0 to 2; this range contains the three lowest numbers in the array.
20
20
- And continue until no numbers remain to be sorted.
21
21
22
-
It nis called a "selection" sort because at every step you search through the rest of the array to select the next lowest number.
22
+
It is called a "selection" sort because at every step you search through the rest of the array to select the next lowest number.
23
23
24
24
## An example
25
25
@@ -108,7 +108,7 @@ The source file [SelectionSort.swift](SelectionSort.swift) has a version of this
108
108
109
109
## Performance
110
110
111
-
The selection sort is easy to understand but it performs slow as **O(n^2)**. It is worse than [insertion sort](../Insertion%20Sort/) but better than [bubble sort](../Bubble%20Sort/). Finding the lowest element in the rest of the array is slow, especially since the inner loop will be performed repeatedly.
111
+
The selection sort is easy to understand but it performs slow as **O(n^2)**. It is worse than [insertion sort](../Insertion%20Sort/) but better than [bubble sort](../Bubble%20Sort/). Finding the lowest element in the rest of the array is slow, especially since the inner loop will be performed repeatedly.
112
112
113
113
The [Heap sort](../Heap%20Sort/) uses the same principle as selection sort but has a fast method for finding the minimum value in the rest of the array. The heap sort' performance is **O(n log n)**.
0 commit comments