Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Merge Sort/MergeSort.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* 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..<middleIndex]))
let rightArray = mergeSort(Array(array[middleIndex..<array.count]))
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]()
Expand Down Expand Up @@ -48,7 +48,7 @@ let sortedArray = mergeSort(array)

/* Bottom-up iterative version */

func mergeSortBottomUp<T>(a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
func mergeSortBottomUp<T>(_ 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
Expand Down
6 changes: 3 additions & 3 deletions Merge Sort/MergeSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
//
//

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..<middleIndex]))
let rightArray = mergeSort(Array(array[middleIndex..<array.count]))
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]()
Expand Down Expand Up @@ -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<T>(a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
func mergeSortBottomUp<T>(_ 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
Expand Down
6 changes: 3 additions & 3 deletions Merge Sort/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<T>(a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
func mergeSortBottomUp<T>(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
let n = a.count

var z = [a, a] // 1
Expand Down