diff --git a/Merge Sort/MergeSort.playground/Contents.swift b/Merge Sort/MergeSort.playground/Contents.swift index a71a90136..ac166131e 100644 --- a/Merge Sort/MergeSort.playground/Contents.swift +++ b/Merge Sort/MergeSort.playground/Contents.swift @@ -1,6 +1,6 @@ /* Top-down recursive version */ -func mergeSort(array: [Int]) -> [Int] { +func mergeSort(_ array: [Int]) -> [Int] { guard array.count > 1 else { return array } let middleIndex = array.count / 2 let leftArray = mergeSort(Array(array[0.. [Int] { return merge(leftPile: leftArray, rightPile: rightArray) } -func merge(leftPile leftPile: [Int], rightPile: [Int]) -> [Int] { +func merge(leftPile: [Int], rightPile: [Int]) -> [Int] { var leftIndex = 0 var rightIndex = 0 var orderedPile = [Int]() @@ -48,7 +48,7 @@ let sortedArray = mergeSort(array) /* Bottom-up iterative version */ -func mergeSortBottomUp(a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] { +func mergeSortBottomUp(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] { let n = a.count var z = [a, a] // the two working arrays var d = 0 // z[d] is used for reading, z[1 - d] for writing diff --git a/Merge Sort/MergeSort.swift b/Merge Sort/MergeSort.swift index 968484dd1..20316663f 100644 --- a/Merge Sort/MergeSort.swift +++ b/Merge Sort/MergeSort.swift @@ -6,7 +6,7 @@ // // -func mergeSort(array: [Int]) -> [Int] { +func mergeSort(_ array: [Int]) -> [Int] { guard array.count > 1 else { return array } let middleIndex = array.count / 2 let leftArray = mergeSort(Array(array[0.. [Int] { return merge(leftPile: leftArray, rightPile: rightArray) } -func merge(leftPile leftPile: [Int], rightPile: [Int]) -> [Int] { +func merge(leftPile: [Int], rightPile: [Int]) -> [Int] { var leftIndex = 0 var rightIndex = 0 var orderedPile = [Int]() @@ -59,7 +59,7 @@ func merge(leftPile leftPile: [Int], rightPile: [Int]) -> [Int] { To avoid allocating many temporary array objects, it uses double-buffering with just two arrays. */ -func mergeSortBottomUp(a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] { +func mergeSortBottomUp(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] { let n = a.count var z = [a, a] // the two working arrays var d = 0 // z[d] is used for reading, z[1 - d] for writing diff --git a/Merge Sort/README.markdown b/Merge Sort/README.markdown index a24c71f2f..3ff69fdd5 100644 --- a/Merge Sort/README.markdown +++ b/Merge Sort/README.markdown @@ -42,7 +42,7 @@ You're left with only two piles and `[9]` finally gets its chance to merge, resu Here's what merge sort may look like in Swift: ```swift -func mergeSort(array: [Int]) -> [Int] { +func mergeSort(_ array: [Int]) -> [Int] { guard array.count > 1 else { return array } // 1 let middleIndex = array.count / 2 // 2 @@ -70,7 +70,7 @@ A step-by-step explanation of how the code works: Here's the merging algorithm: ```swift -func merge(leftPile leftPile: [Int], rightPile: [Int]) -> [Int] { +func merge(leftPile: [Int], rightPile: [Int]) -> [Int] { // 1 var leftIndex = 0 var rightIndex = 0 @@ -162,7 +162,7 @@ The implementation of merge sort you've seen so far is called "top-down" because Time to step up the game a little. :-) Here is a complete bottom-up implementation in Swift: ```swift -func mergeSortBottomUp(a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] { +func mergeSortBottomUp(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] { let n = a.count var z = [a, a] // 1