Skip to content

Commit e0cdabe

Browse files
authored
Merge pull request thuva4#311 from FernandaOchoa/FernandaOchoa-MergeSort
MergeSort.swift
2 parents 3951aa6 + 002614e commit e0cdabe

3 files changed

Lines changed: 108 additions & 2 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,4 @@ Unfortunately, sometimes the bug can be only reproduced in your project or in yo
144144
- [pranjalrai](https://github.com/pranjalrai)
145145
- [stuxxnet](https://github.com/stuxxnet42)
146146
- [BurnzZ](https://github.com/BurnzZ)
147-
- [FernandaOchoa](http://github.com/FernandaOchoa)
147+
- [FernandaOchoa](http://github.com/FernandaOchoa)

MergeSort/Swift/MergeSort.swift

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// Mergesort.swift
3+
//
4+
//
5+
// Created by Kelvin Lau on 2016-02-03.
6+
//
7+
//
8+
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)
15+
}
16+
17+
func merge<T: Comparable>(leftPile: [T], rightPile: [T]) -> [T] {
18+
var leftIndex = 0
19+
var rightIndex = 0
20+
var orderedPile: [T] = []
21+
if orderedPile.capacity < leftPile.count + rightPile.count {
22+
orderedPile.reserveCapacity(leftPile.count + rightPile.count)
23+
}
24+
25+
while true {
26+
guard leftIndex < leftPile.endIndex else {
27+
orderedPile.append(contentsOf: rightPile[rightIndex..<rightPile.endIndex])
28+
break
29+
}
30+
guard rightIndex < rightPile.endIndex else {
31+
orderedPile.append(contentsOf: leftPile[leftIndex..<leftPile.endIndex])
32+
break
33+
}
34+
35+
if leftPile[leftIndex] < rightPile[rightIndex] {
36+
orderedPile.append(leftPile[leftIndex])
37+
leftIndex += 1
38+
} else {
39+
orderedPile.append(rightPile[rightIndex])
40+
rightIndex += 1
41+
}
42+
}
43+
44+
45+
return orderedPile
46+
}
47+
48+
/*
49+
This is an iterative bottom-up implementation. Instead of recursively splitting
50+
up the array into smaller sublists, it immediately starts merging the individual
51+
array elements.
52+
53+
As the algorithm works its way up, it no longer merges individual elements but
54+
larger and larger subarrays, until eventually the entire array is merged and
55+
sorted.
56+
57+
To avoid allocating many temporary array objects, it uses double-buffering with
58+
just two arrays.
59+
*/
60+
func mergeSortBottomUp<T>(_ a: [T], _ isOrderedBefore: (T, T) -> Bool) -> [T] {
61+
let n = a.count
62+
var z = [a, a] // the two working arrays
63+
var d = 0 // z[d] is used for reading, z[1 - d] for writing
64+
65+
var width = 1
66+
while width < n {
67+
68+
var i = 0
69+
while i < n {
70+
71+
var j = i
72+
var l = i
73+
var r = i + width
74+
75+
let lmax = min(l + width, n)
76+
let rmax = min(r + width, n)
77+
78+
while l < lmax && r < rmax {
79+
if isOrderedBefore(z[d][l], z[d][r]) {
80+
z[1 - d][j] = z[d][l]
81+
l += 1
82+
} else {
83+
z[1 - d][j] = z[d][r]
84+
r += 1
85+
}
86+
j += 1
87+
}
88+
while l < lmax {
89+
z[1 - d][j] = z[d][l]
90+
j += 1
91+
l += 1
92+
}
93+
while r < rmax {
94+
z[1 - d][j] = z[d][r]
95+
j += 1
96+
r += 1
97+
}
98+
99+
i += width*2
100+
}
101+
102+
width *= 2 // in each step, the subarray to merge becomes larger
103+
d = 1 - d // swap active array
104+
}
105+
return z[d]
106+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LinearSearch | :+1: | :+1: | | :+1:| :+1: | :+1: | | | | :+1: |
4141
Longest-Common-Subsequence | :+1: | :+1: | | | :+1: | | | | :+1:
4242
Longest-Increasing-Subsequence | :+1: | | | | :+1: | | | |
4343
LongestPath | | | | | :+1: | | | |
44-
MergeSort | :+1: | :+1: | | :+1: | :+1: | :+1: | :+1: | |
44+
MergeSort | :+1: | :+1: | | :+1: | :+1: | :+1: | :+1: | | | :+1:
4545
MiniMaxWithABPruning | :+1: | | | | | | | |
4646
Modified_Binary_Search | | | | :+1: | | | | |
4747
Pearson Hashing | :+1: | | | | | | | |

0 commit comments

Comments
 (0)