-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
algorithms/sorting/merge-sort-recursive/merge-sort-inplace.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Recursive merge sort implementation in JavaScript | ||
* Copyright (c) 2012 Nicholas C. Zakas | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* Merges to arrays in order based on their natural | ||
* relationship. | ||
* @param {Array} left The first array to merge. | ||
* @param {Array} right The second array to merge. | ||
* @return {Array} The merged array. | ||
*/ | ||
function merge(left, right){ | ||
var result = [], | ||
il = 0, | ||
ir = 0; | ||
|
||
while (il < left.length && ir < right.length){ | ||
if (left[il] < right[ir]){ | ||
result.push(left[il++]); | ||
} else { | ||
result.push(right[ir++]); | ||
} | ||
} | ||
|
||
return result.concat(left.slice(il)).concat(right.slice(ir)); | ||
} | ||
|
||
/** | ||
* Sorts an array in ascending natural order using | ||
* merge sort. | ||
* @param {Array} items The array to sort. | ||
* @return {Array} The sorted array. | ||
*/ | ||
function mergeSort(items){ | ||
|
||
if (items.length < 2) { | ||
return items; | ||
} | ||
|
||
var middle = Math.floor(items.length / 2), | ||
left = items.slice(0, middle), | ||
right = items.slice(middle), | ||
params = merge(mergeSort(left), mergeSort(right)); | ||
|
||
// Add the arguments to replace everything between 0 and last item in the array | ||
params.unshift(0, items.length); | ||
items.splice.apply(items, params); | ||
return items; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Insertion sort implementation in JavaScript | ||
* Copyright (c) 2012 Nicholas C. Zakas | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of items software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and items permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* Swaps two values in an array. | ||
* @param {Array} items The array containing the items. | ||
* @param {int} firstIndex Index of first item to swap. | ||
* @param {int} secondIndex Index of second item to swap. | ||
* @return {void} | ||
*/ | ||
function swap(items, firstIndex, secondIndex){ | ||
var temp = items[firstIndex]; | ||
items[firstIndex] = items[secondIndex]; | ||
items[secondIndex] = temp; | ||
} | ||
|
||
function partition(items, left, right) { | ||
|
||
var pivot = items[Math.ceil((right + left) / 2)], // pivot value is middle item | ||
i = left, // starts from left and goes right to pivot index | ||
j = right; // starts from right and goes left to pivot index | ||
|
||
|
||
// while the two indices don't match | ||
while (i <= j) { | ||
|
||
// if the item on the left is less than the pivot, continue right | ||
while (items[i] < pivot) { | ||
i++; | ||
} | ||
|
||
// if the item on the right is greater than the pivot, continue left | ||
while (items[j] > pivot) { | ||
j--; | ||
} | ||
|
||
// if the two indices still don't match, swap the values | ||
if (i <= j) { | ||
swap(items, i, j); | ||
|
||
// change indices to continue loop | ||
i++; | ||
j--; | ||
} | ||
} | ||
|
||
// this value is necessary for recursion | ||
return i; | ||
} | ||
|
||
/** | ||
* A quicksort implementation in JavaScript. The array | ||
* is sorted in place. | ||
* @param {Array} items An array of items to sort. | ||
* @return {Array} The sorted array. | ||
*/ | ||
function quickSort(items, left, right) { | ||
|
||
var index; | ||
|
||
// performance - don't sort an array with zero or one items | ||
if (items.length > 1) { | ||
|
||
// fix left and right values - might not be provided | ||
left = typeof left != "number" ? 0 : left; | ||
right = typeof right != "number" ? items.length - 1 : right; | ||
|
||
// split up the entire array | ||
index = partition(items, left, right); | ||
|
||
// if the returned index | ||
if (left < index - 1) { | ||
quickSort(items, left, index - 1); | ||
} | ||
|
||
if (index < right) { | ||
quickSort(items, index + 1, right); | ||
} | ||
|
||
} | ||
|
||
return items; | ||
} | ||
|