|
| 1 | +/* |
| 2 | + * Insertion sort implementation in JavaScript |
| 3 | + * Copyright (c) 2012 Nicholas C. Zakas |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of items software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and items permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * Swaps two values in an array. |
| 26 | + * @param {Array} items The array containing the items. |
| 27 | + * @param {int} firstIndex Index of first item to swap. |
| 28 | + * @param {int} secondIndex Index of second item to swap. |
| 29 | + * @return {void} |
| 30 | + */ |
| 31 | +function swap(items, firstIndex, secondIndex){ |
| 32 | + var temp = items[firstIndex]; |
| 33 | + items[firstIndex] = items[secondIndex]; |
| 34 | + items[secondIndex] = temp; |
| 35 | +} |
| 36 | + |
| 37 | +function partition(items, left, right) { |
| 38 | + |
| 39 | + var pivot = items[Math.ceil((right + left) / 2)], // pivot value is middle item |
| 40 | + i = left, // starts from left and goes right to pivot index |
| 41 | + j = right; // starts from right and goes left to pivot index |
| 42 | + |
| 43 | + |
| 44 | + // while the two indices don't match |
| 45 | + while (i <= j) { |
| 46 | + |
| 47 | + // if the item on the left is less than the pivot, continue right |
| 48 | + while (items[i] < pivot) { |
| 49 | + i++; |
| 50 | + } |
| 51 | + |
| 52 | + // if the item on the right is greater than the pivot, continue left |
| 53 | + while (items[j] > pivot) { |
| 54 | + j--; |
| 55 | + } |
| 56 | + |
| 57 | + // if the two indices still don't match, swap the values |
| 58 | + if (i <= j) { |
| 59 | + swap(items, i, j); |
| 60 | + |
| 61 | + // change indices to continue loop |
| 62 | + i++; |
| 63 | + j--; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // this value is necessary for recursion |
| 68 | + return i; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * A quicksort implementation in JavaScript. The array |
| 73 | + * is sorted in place. |
| 74 | + * @param {Array} items An array of items to sort. |
| 75 | + * @return {Array} The sorted array. |
| 76 | + */ |
| 77 | +function quickSort(items, left, right) { |
| 78 | + |
| 79 | + var index; |
| 80 | + |
| 81 | + // performance - don't sort an array with zero or one items |
| 82 | + if (items.length > 1) { |
| 83 | + |
| 84 | + // fix left and right values - might not be provided |
| 85 | + left = typeof left != "number" ? 0 : left; |
| 86 | + right = typeof right != "number" ? items.length - 1 : right; |
| 87 | + |
| 88 | + // split up the entire array |
| 89 | + index = partition(items, left, right); |
| 90 | + |
| 91 | + // if the returned index |
| 92 | + if (left < index - 1) { |
| 93 | + quickSort(items, left, index - 1); |
| 94 | + } |
| 95 | + |
| 96 | + if (index < right) { |
| 97 | + quickSort(items, index + 1, right); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + return items; |
| 103 | +} |
| 104 | + |
0 commit comments