Skip to content

Commit 8284809

Browse files
authored
Merge pull request #856 from amakukha/master
reserveCapacity missing in README for MergeSort
2 parents b80f6e4 + fa656f0 commit 8284809

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Merge Sort/README.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func merge(leftPile: [Int], rightPile: [Int]) -> [Int] {
7979

8080
// 2
8181
var orderedPile = [Int]()
82+
orderedPile.reserveCapacity(leftPile.count + rightPile.count)
8283

8384
// 3
8485
while leftIndex < leftPile.count && rightIndex < rightPile.count {
@@ -115,7 +116,7 @@ This method may look scary, but it is quite straightforward:
115116

116117
1. You need two indexes to keep track of your progress for the two arrays while merging.
117118

118-
2. This is the merged array. It is empty right now, but you will build it up in subsequent steps by appending elements from the other arrays.
119+
2. This is the merged array. It is empty right now, but you will build it up in subsequent steps by appending elements from the other arrays. Since you already know number of elements that will end up in this array, you reserve capacity to avoid reallocation overhead later.
119120

120121
3. This while-loop will compare the elements from the left and right sides and append them into the `orderedPile` while making sure that the result stays in order.
121122

0 commit comments

Comments
 (0)