Skip to content

Commit 3d611c1

Browse files
committed
Fixed merge sort, added quicksort
1 parent 3f7b8fa commit 3d611c1

File tree

3 files changed

+181
-3
lines changed

3 files changed

+181
-3
lines changed

algorithms/sorting/merge-sort-iterative/merge-sort-iterative.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,18 @@ function merge(left, right){
5555
* @return {Array} The sorted array.
5656
*/
5757
function mergeSort(items){
58-
if (items.length == 1) {
58+
59+
// Terminal condition - don't need to do anything for arrays with 0 or 1 items
60+
if (items.length < 2) {
5961
return items;
6062
}
6163

62-
var work = [];
63-
for (var i=0, len=items.length; i < len; i++){
64+
var work = [],
65+
i,
66+
len;
67+
68+
69+
for (i=0, len=items.length; i < len; i++){
6470
work.push([items[i]]);
6571
}
6672
work.push([]); //in case of odd number of items
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Recursive merge 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 this 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 this 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+
* Merges to arrays in order based on their natural
26+
* relationship.
27+
* @param {Array} left The first array to merge.
28+
* @param {Array} right The second array to merge.
29+
* @return {Array} The merged array.
30+
*/
31+
function merge(left, right){
32+
var result = [],
33+
il = 0,
34+
ir = 0;
35+
36+
while (il < left.length && ir < right.length){
37+
if (left[il] < right[ir]){
38+
result.push(left[il++]);
39+
} else {
40+
result.push(right[ir++]);
41+
}
42+
}
43+
44+
return result.concat(left.slice(il)).concat(right.slice(ir));
45+
}
46+
47+
/**
48+
* Sorts an array in ascending natural order using
49+
* merge sort.
50+
* @param {Array} items The array to sort.
51+
* @return {Array} The sorted array.
52+
*/
53+
function mergeSort(items){
54+
55+
if (items.length < 2) {
56+
return items;
57+
}
58+
59+
var middle = Math.floor(items.length / 2),
60+
left = items.slice(0, middle),
61+
right = items.slice(middle),
62+
params = merge(mergeSort(left), mergeSort(right));
63+
64+
// Add the arguments to replace everything between 0 and last item in the array
65+
params.unshift(0, items.length);
66+
items.splice.apply(items, params);
67+
return items;
68+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)