Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented MergeSort.

  • Loading branch information...
commit baafa945fb765117777cdd564f1c938f85bfe85a 1 parent 86d349e
@JefClaes authored
View
64 JavaScriptAlgorithmsDataStructs/Scripts/Algorithms.js
@@ -6,17 +6,58 @@
var tmp = elements[one];
elements[one] = elements[two];
elements[two] = tmp;
- };
+ };
+
+ internalMergeSort = function(elements, onSort){
+ if (elements.length < 2){
+ return elements;
+ }
+
+ // Calculate the middle of the elements
+ var middle = Math.floor(elements.length / 2);
+
+ // Divide
+ var leftRange = elements.slice(0, middle);
+ var rightRange = elements.slice(middle, elements.length);
+
+ // Conquer
+ var mergingResult = merge(internalMergeSort(leftRange, onSort),
+ internalMergeSort(rightRange, onSort));
+
+ onSort(mergingResult);
+
+ return mergingResult;
+ };
+ function merge(left, right){
+ var res = [];
+
+ while (left.length > 0 && right.length > 0) {
+ if (left[0] <= right[0]) {
+ res.push(left.shift());
+ } else {
+ res.push(right.shift());
+ }
+ }
+
+ while (left.length > 0) {
+ res.push(left.shift());
+ }
+ while (right.length > 0) {
+ res.push(right.shift());
+ }
+
+ return res;
+ };
+
this.onSort = function() {};
this.push = function(val){
elements.push(val);
- };
+ };
this.bubbleSort = function() {
this.onSort(elements);
//Loop over all the elements
- for (var out = elements.length - 1; out > 0; out--){
-
+ for (var out = elements.length - 1; out > 0; out--){
for (var inn = 0; inn < out; inn++) {
//Are they out of order?
if (elements[inn] > elements[inn+1]){
@@ -24,7 +65,9 @@
}
}
this.onSort(elements);
- }
+ }
+
+ return elements;
};
this.selectionSort = function(){
this.onSort(elements);
@@ -39,6 +82,8 @@
swap(out, min);
this.onSort(elements);
}
+
+ return elements;
};
this.insertionSort = function(){
this.onSort(elements);
@@ -54,6 +99,13 @@
elements[inn] = temp;
this.onSort(elements);
}
- }
+
+ return elements;
+ };
+ this.mergeSort = function() {
+ elements = internalMergeSort(elements, this.onSort);
+
+ return elements;
+ };
}
}
View
42 JavaScriptAlgorithmsDataStructs/Tests/MergeSort.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <title>MergeSort</title>
+ <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.min.js"></script>
+ <script type="text/javascript" src="http://code.jquery.com/qunit/qunit-git.js"></script>
+ <script type="text/javascript" src="../Scripts/Algorithms.js"></script>
+ <link rel="stylesheet" type="text/css" href="http://code.jquery.com/qunit/qunit-git.css"></style>
+ <script type="text/javascript">
+ test("MergeSort sorts.", function() {
+ var sortArray = new algorithms.sortArray();
+
+ sortArray.push(15);
+ sortArray.push(50);
+ sortArray.push(60);
+ sortArray.push(1);
+ sortArray.push(22);
+ sortArray.push(90);
+ sortArray.push(10);
+ sortArray.push(12);
+ sortArray.push(25);
+ sortArray.push(17);
+ sortArray.push(130);
+
+ var actual = sortArray.mergeSort();
+ var expected = [1, 10, 12, 15, 17, 22, 25, 50, 60, 90, 130];
+
+ deepEqual(actual, expected);
+ });
+ </script>
+ </head>
+ <body>
+ <h1 id="qunit-header">LinkedList implementation</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-fixture">test markup, will be hidden</div>
+ </body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.