Last active
August 29, 2015 14:16
-
-
Save bgourlie/251863137b432032e061 to your computer and use it in GitHub Desktop.
Implementation of mergesort in dart
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
void main() { | |
final l1 = [1,3,5,9,11, 23,201,202]; | |
final l2 = [2,2,3,9,10,300,301,415,500]; | |
final a = new List<int>(l1.length + l2.length); | |
_merge(l1, l2, a); | |
print(a); | |
final sorted = [99,54,10534,42,9,2,4,4,23,54,62,88,33,101,1,2,2,1,7,4,77,6,143]; | |
mergeSort(sorted); | |
print(sorted); | |
} | |
void _merge(List<int> left, List<int> right, List<int> a){ | |
assert(a.length == left.length + right.length); | |
final leftBound = left.length; | |
final rightBound = right.length; | |
final total = leftBound + rightBound; | |
int i = 0; | |
int curL = 0; | |
int curR = 0; | |
while(curL + curR < total){ | |
if(curL >= leftBound){ | |
for(int j = curR; j < rightBound; j++){ | |
a[i++] = right[curR++]; | |
} | |
} else if (curR >= rightBound){ | |
for(int j = curL; j < leftBound; j++){ | |
a[i++] = left[curL++]; | |
} | |
}else{ | |
if(left[curL] < right[curR]){ | |
a[i++] = left[curL++]; | |
} else { | |
a[i++] = right[curR++]; | |
} | |
} | |
} | |
if(total == 2){ | |
if(a[0] > a[1]){ | |
final tmp = a[0]; | |
a[0] = a[1]; | |
a[1] = tmp; | |
} | |
} | |
} | |
void mergeSort(List<int> array){ | |
final arrayBound = array.length; | |
if(arrayBound < 2) { | |
return; | |
} | |
final isOdd = arrayBound % 2 == 1; | |
final half = (arrayBound / 2).floor(); | |
final left = new List(half); | |
final rBound = half + (isOdd ? 1 : 0); | |
final right = new List(rBound); | |
for(int i = 0; i < half; i++){ | |
left[i] = array[i]; | |
right[i] = array[i + half]; | |
} | |
if(isOdd){ | |
right[rBound - 1] = array[arrayBound - 1]; | |
} | |
mergeSort(left); | |
mergeSort(right); | |
_merge(left, right, array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment