Skip to content

Commit 797a2e9

Browse files
committed
Updated Merge Sort (Recursive) to Generic.
1 parent 3c5a105 commit 797a2e9

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

Merge Sort/MergeSort.playground/Contents.swift

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

3-
func mergeSort(_ array: [Int]) -> [Int] {
3+
func mergeSort<T: Comparable>(_ array: [T]) -> [T] {
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: [Int], rightPile: [Int]) -> [Int] {
11+
func merge<T: Comparable>(leftPile: [T], rightPile: [T]) -> [T] {
1212
var leftIndex = 0
1313
var rightIndex = 0
14-
var orderedPile = [Int]()
14+
var orderedPile = [T]()
1515

1616
while leftIndex < leftPile.count && rightIndex < rightPile.count {
1717
if leftPile[leftIndex] < rightPile[rightIndex] {

Merge Sort/MergeSort.swift

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

9-
func mergeSort(_ array: [Int]) -> [Int] {
10-
guard array.count > 1 else { return array }
11-
let middleIndex = array.count / 2
12-
let leftArray = mergeSort(Array(array[0..<middleIndex]))
13-
let rightArray = mergeSort(Array(array[middleIndex..<array.count]))
14-
return merge(leftPile: leftArray, rightPile: rightArray)
9+
func mergeSort<T: Comparable>(_ array: [T]) -> [T] {
10+
guard array.count > 1 else { return array }
11+
let middleIndex = array.count / 2
12+
let leftArray = mergeSort(Array(array[0..<middleIndex]))
13+
let rightArray = mergeSort(Array(array[middleIndex..<array.count]))
14+
return merge(leftPile: leftArray, rightPile: rightArray)
1515
}
1616

17-
func merge(leftPile: [Int], rightPile: [Int]) -> [Int] {
18-
var leftIndex = 0
19-
var rightIndex = 0
20-
var orderedPile = [Int]()
21-
22-
while leftIndex < leftPile.count && rightIndex < rightPile.count {
23-
if leftPile[leftIndex] < rightPile[rightIndex] {
24-
orderedPile.append(leftPile[leftIndex])
25-
leftIndex += 1
26-
} else if leftPile[leftIndex] > rightPile[rightIndex] {
27-
orderedPile.append(rightPile[rightIndex])
28-
rightIndex += 1
29-
} else {
30-
orderedPile.append(leftPile[leftIndex])
31-
leftIndex += 1
32-
orderedPile.append(rightPile[rightIndex])
33-
rightIndex += 1
17+
func merge<T: Comparable>(leftPile: [T], rightPile: [T]) -> [T] {
18+
var leftIndex = 0
19+
var rightIndex = 0
20+
var orderedPile = [T]()
21+
22+
while leftIndex < leftPile.count && rightIndex < rightPile.count {
23+
if leftPile[leftIndex] < rightPile[rightIndex] {
24+
orderedPile.append(leftPile[leftIndex])
25+
leftIndex += 1
26+
} else if leftPile[leftIndex] > rightPile[rightIndex] {
27+
orderedPile.append(rightPile[rightIndex])
28+
rightIndex += 1
29+
} else {
30+
orderedPile.append(leftPile[leftIndex])
31+
leftIndex += 1
32+
orderedPile.append(rightPile[rightIndex])
33+
rightIndex += 1
34+
}
3435
}
35-
}
36-
37-
while leftIndex < leftPile.count {
38-
orderedPile.append(leftPile[leftIndex])
39-
leftIndex += 1
40-
}
41-
42-
while rightIndex < rightPile.count {
43-
orderedPile.append(rightPile[rightIndex])
44-
rightIndex += 1
45-
}
46-
47-
return orderedPile
36+
37+
while leftIndex < leftPile.count {
38+
orderedPile.append(leftPile[leftIndex])
39+
leftIndex += 1
40+
}
41+
42+
while rightIndex < rightPile.count {
43+
orderedPile.append(rightPile[rightIndex])
44+
rightIndex += 1
45+
}
46+
47+
return orderedPile
4848
}
4949

5050
/*

0 commit comments

Comments
 (0)