forked from TheAlgorithms/Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#24 from codersanjeev/master
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
import Foundation | ||
|
||
extension Array where Element: Comparable { | ||
|
||
mutating func mergeSort(by comparison: (Element, Element) -> Bool) { | ||
guard self.count > 1 else { | ||
return | ||
} | ||
_mergeSort(from: 0, to: count - 1, by: comparison) | ||
} | ||
|
||
mutating private func _mergeSort( | ||
from left: Int, | ||
to right: Int, | ||
by comparison: (Element, Element) -> Bool | ||
) { | ||
if left < right { | ||
let mid = left + (right - left) / 2 | ||
_mergeSort(from: 0, to: mid, by: comparison) | ||
_mergeSort(from: mid + 1, to: right, by: comparison) | ||
_merge(from: left, mid: mid, to: right, by: comparison) | ||
} | ||
} | ||
|
||
mutating private func _merge( | ||
from left: Int, | ||
mid: Int, | ||
to right: Int, | ||
by comparison: (Element, Element) -> Bool | ||
) { | ||
var copy = [Element](repeating: self[left], count: right - left + 1) | ||
var (leftStartIndex, rightStartIndex, currentIndex) = (left, mid + 1, 0) | ||
for _ in left ... right { | ||
if leftStartIndex > mid { | ||
copy[currentIndex] = self[rightStartIndex] | ||
rightStartIndex += 1 | ||
} else if rightStartIndex > right { | ||
copy[currentIndex] = self[leftStartIndex] | ||
leftStartIndex += 1 | ||
} else if comparison(self[leftStartIndex], self[rightStartIndex]) { | ||
copy[currentIndex] = self[leftStartIndex] | ||
leftStartIndex += 1 | ||
} else { | ||
copy[currentIndex] = self[rightStartIndex] | ||
rightStartIndex += 1 | ||
} | ||
currentIndex += 1 | ||
} | ||
leftStartIndex = left | ||
for i in copy.indices { | ||
self[leftStartIndex] = copy[i] | ||
leftStartIndex += 1 | ||
} | ||
} | ||
|
||
func mergeSorted(by comparison: (Element, Element) -> Bool) -> Array { | ||
var copy = self | ||
copy.mergeSort(by: comparison) | ||
return copy | ||
} | ||
|
||
} | ||
|
||
// The code below can be used for testing | ||
// var numberList = [15, 2, 23, 11, 3, 9] | ||
// debugPrint(numberList.mergeSorted(by: >)) | ||
// numberList.mergeSort(by: <) | ||
// debugPrint(numberList) |