Skip to content

Commit b683b21

Browse files
authored
Merge pull request #657 from artkirillov/master
Fix typos in Merge Sort and Selection Sort articles
2 parents cc338d6 + bdedbff commit b683b21

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

Merge Sort/README.markdown

+19-19
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Goal: Sort an array from low to high (or high to low)
66

77
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)**.
88

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**.
1010

1111
Assume you need to sort an array of *n* numbers in the right order. The merge-sort algorithm works as follows:
1212

@@ -19,25 +19,25 @@ Assume you need to sort an array of *n* numbers in the right order. The merge-so
1919

2020
### Splitting
2121

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

2424
First, split the array into two halves: `[2, 1]` and `[5, 4, 9]`. Can you keep splitting them? Yes, you can!
2525

2626
Focus on the left pile. Split`[2, 1]` into `[2]` and `[1]`. Can you keep splitting them? No. Time to check the other pile.
2727

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

3030
The splitting process ends with the following piles: `[2]` `[1]` `[5]` `[4]` `[9]`. Notice that each pile consists of just one element.
3131

3232
### Merging
3333

3434
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.
3535

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

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

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

4242
## Top-down implementation
4343

@@ -61,7 +61,7 @@ A step-by-step explanation of how the code works:
6161

6262
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.
6363

64-
2. Find the middle index.
64+
2. Find the middle index.
6565

6666
3. Using the middle index from the previous step, recursively split the left side of the array.
6767

@@ -77,7 +77,7 @@ func merge(leftPile: [Int], rightPile: [Int]) -> [Int] {
7777
var leftIndex = 0
7878
var rightIndex = 0
7979

80-
// 2
80+
// 2
8181
var orderedPile = [Int]()
8282

8383
// 3
@@ -144,16 +144,16 @@ This process repeats. At each step, we pick the smallest item from either the `l
144144
leftPile rightPile orderedPile
145145
[ 1, 7, 8 ] [ 3, 6, 9 ] [ 1, 3, 6 ]
146146
l -->r
147-
147+
148148
leftPile rightPile orderedPile
149149
[ 1, 7, 8 ] [ 3, 6, 9 ] [ 1, 3, 6, 7 ]
150150
-->l r
151-
151+
152152
leftPile rightPile orderedPile
153153
[ 1, 7, 8 ] [ 3, 6, 9 ] [ 1, 3, 6, 7, 8 ]
154154
-->l r
155155

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

158158
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.
159159

@@ -169,20 +169,20 @@ func mergeSortBottomUp<T>(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
169169

170170
var z = [a, a] // 1
171171
var d = 0
172-
172+
173173
var width = 1
174174
while width < n { // 2
175-
175+
176176
var i = 0
177177
while i < n { // 3
178178

179179
var j = i
180180
var l = i
181181
var r = i + width
182-
182+
183183
let lmax = min(l + width, n)
184184
let rmax = min(r + width, n)
185-
185+
186186
while l < lmax && r < rmax { // 4
187187
if isOrderedBefore(z[d][l], z[d][r]) {
188188
z[1 - d][j] = z[d][l]
@@ -206,7 +206,7 @@ func mergeSortBottomUp<T>(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
206206

207207
i += width*2
208208
}
209-
209+
210210
width *= 2
211211
d = 1 - d // 5
212212
}
@@ -239,11 +239,11 @@ mergeSortBottomUp(array, <) // [1, 2, 4, 5, 9]
239239

240240
## Performance
241241

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

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

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)**.
247247

248248
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/).
249249

Selection Sort/README.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ It works as follows:
1919
- 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.
2020
- And continue until no numbers remain to be sorted.
2121

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

2424
## An example
2525

@@ -108,7 +108,7 @@ The source file [SelectionSort.swift](SelectionSort.swift) has a version of this
108108

109109
## Performance
110110

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

113113
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)**.
114114

0 commit comments

Comments
 (0)