Skip to content

Commit dca75c0

Browse files
author
Chris Pilcher
authored
Merge pull request kodecocodes#199 from nemanjavlahovic/merge-sort-nemanja
Updating Merge Sort to Swift 3
2 parents 59314d4 + 283ba84 commit dca75c0

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

Merge Sort/MergeSort.playground/Contents.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/* Top-down recursive version */
22

3-
func mergeSort(array: [Int]) -> [Int] {
3+
func mergeSort(_ array: [Int]) -> [Int] {
44
guard array.count > 1 else { return array }
55
let middleIndex = array.count / 2
66
let leftArray = mergeSort(Array(array[0..<middleIndex]))
77
let rightArray = mergeSort(Array(array[middleIndex..<array.count]))
88
return merge(leftPile: leftArray, rightPile: rightArray)
99
}
1010

11-
func merge(leftPile leftPile: [Int], rightPile: [Int]) -> [Int] {
11+
func merge(leftPile: [Int], rightPile: [Int]) -> [Int] {
1212
var leftIndex = 0
1313
var rightIndex = 0
1414
var orderedPile = [Int]()
@@ -48,7 +48,7 @@ let sortedArray = mergeSort(array)
4848

4949
/* Bottom-up iterative version */
5050

51-
func mergeSortBottomUp<T>(a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
51+
func mergeSortBottomUp<T>(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
5252
let n = a.count
5353
var z = [a, a] // the two working arrays
5454
var d = 0 // z[d] is used for reading, z[1 - d] for writing

Merge Sort/MergeSort.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
//
77
//
88

9-
func mergeSort(array: [Int]) -> [Int] {
9+
func mergeSort(_ array: [Int]) -> [Int] {
1010
guard array.count > 1 else { return array }
1111
let middleIndex = array.count / 2
1212
let leftArray = mergeSort(Array(array[0..<middleIndex]))
1313
let rightArray = mergeSort(Array(array[middleIndex..<array.count]))
1414
return merge(leftPile: leftArray, rightPile: rightArray)
1515
}
1616

17-
func merge(leftPile leftPile: [Int], rightPile: [Int]) -> [Int] {
17+
func merge(leftPile: [Int], rightPile: [Int]) -> [Int] {
1818
var leftIndex = 0
1919
var rightIndex = 0
2020
var orderedPile = [Int]()
@@ -59,7 +59,7 @@ func merge(leftPile leftPile: [Int], rightPile: [Int]) -> [Int] {
5959
To avoid allocating many temporary array objects, it uses double-buffering with
6060
just two arrays.
6161
*/
62-
func mergeSortBottomUp<T>(a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
62+
func mergeSortBottomUp<T>(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
6363
let n = a.count
6464
var z = [a, a] // the two working arrays
6565
var d = 0 // z[d] is used for reading, z[1 - d] for writing

Merge Sort/README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ You're left with only two piles and `[9]` finally gets its chance to merge, resu
4242
Here's what merge sort may look like in Swift:
4343

4444
```swift
45-
func mergeSort(array: [Int]) -> [Int] {
45+
func mergeSort(_ array: [Int]) -> [Int] {
4646
guard array.count > 1 else { return array } // 1
4747

4848
let middleIndex = array.count / 2 // 2
@@ -70,7 +70,7 @@ A step-by-step explanation of how the code works:
7070
Here's the merging algorithm:
7171

7272
```swift
73-
func merge(leftPile leftPile: [Int], rightPile: [Int]) -> [Int] {
73+
func merge(leftPile: [Int], rightPile: [Int]) -> [Int] {
7474
// 1
7575
var leftIndex = 0
7676
var rightIndex = 0
@@ -162,7 +162,7 @@ The implementation of merge sort you've seen so far is called "top-down" because
162162
Time to step up the game a little. :-) Here is a complete bottom-up implementation in Swift:
163163

164164
```swift
165-
func mergeSortBottomUp<T>(a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
165+
func mergeSortBottomUp<T>(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
166166
let n = a.count
167167

168168
var z = [a, a] // 1

0 commit comments

Comments
 (0)