Commit fb068b08 authored by Jeffrey Phillips Freeman's avatar Jeffrey Phillips Freeman 💥
Browse files

A javascript implementation of merge sort tested with jasmine.

parents
function mergeSort()
{
var unsortedValues = Array.prototype.slice.call(arguments);
return mergeSortArray(unsortedValues);
}
function mergeSortArray(unsortedValues)
{
if(unsortedValues.length <= 1)
return unsortedValues;
var middleIndex = Math.floor(unsortedValues.length/2);
var bottomValues = unsortedValues.slice(0, middleIndex);
var topValues = unsortedValues.slice(middleIndex, unsortedValues.length);
//if the top or bottom halves have more than one element in them, recursively
//sort them first
if(bottomValues.length > 1)
bottomValues = mergeSortArray(bottomValues);
if(topValues.length > 1)
topValues = mergeSortArray(topValues);
//now is where we merge the two arrays. We have two sorted arrays we want to
//merge into one larger sorted array.
var bottomIndex = 0;
var topIndex = 0;
var sortedIndex = 0;
var sortedValues = new Array(unsortedValues.length);
while( sortedIndex < sortedValues.length )
{
//check if there is just one half of the array left
if(bottomIndex >= bottomValues.length)
{
while(topIndex < topValues.length)
{
sortedValues[sortedIndex] = topValues[topIndex];
topIndex++;
sortedIndex++;
}
}
else if(topIndex >= topValues.length)
{
while(bottomIndex < bottomValues.length)
{
sortedValues[sortedIndex] = bottomValues[bottomIndex];
bottomIndex++;
sortedIndex++;
}
}
//since both arrays are still in play lets do our comparison and sort
else if(bottomValues[bottomIndex] < topValues[topIndex])
{
sortedValues[sortedIndex] = bottomValues[bottomIndex];
sortedIndex++;
bottomIndex++;
}
else
{
sortedValues[sortedIndex] = topValues[topIndex];
sortedIndex++;
topIndex++;
}
}
return sortedValues;
}
\ No newline at end of file
describe("MergeSort", function() {
it("should determine [4,1,3] is sorted as [1,3,4]", function() {
expect(mergeSort(4,1,3)).toEqual(new Array(1,3,4));
});
it("should determine [9,23,576,1,5,8,4,1,3] is sorted as [1,1,3,4,5,8,9,23,576]", function() {
expect(mergeSort(9,23,576,1,5,8,4,1,3)).toEqual(new Array(1,1,3,4,5,8,9,23,576));
});
});
\ No newline at end of file
An implementation of MergeSort, an efficient sorting algorithm. Also Jasmine is
used for unit testing the function.
\ No newline at end of file
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Primality Unit Tests</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-1.3.0/jasmine_favicon.png" />
<!-- include source files here... -->
<link data-require="jasmine@*" data-semver="1.3.1" rel="stylesheet" href="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" />
<script data-require="jasmine@*" data-semver="1.3.1" src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script data-require="jasmine@*" data-semver="1.3.1" src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<script data-require="json2@*" data-semver="0.0.2012100-8" src="//cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.js"></script>
<!-- include src files here... -->
<script type="text/javascript" src="MergeSort.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="MergeSort.spec.js"></script>
<script type="text/javascript">
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
</script>
</head>
<body></body>
</html>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment