Last active
August 29, 2015 14:06
-
-
Save alexhawkins/d8d7e0ef6684cafc4a44 to your computer and use it in GitHub Desktop.
Various Bubble/Selection/Quick/Merge Sort Implementations
This file contains hidden or 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
/* BASIC BUBBLE SORT ALGORITHM | |
1. Compare first item to the second item | |
2. Swap if first item should be after second | |
3. Compare second to third item | |
4. If second should be after third, swap | |
5. Continue until end of data set; | |
*/ | |
//FAST BUBBLE SORT | |
var bubbleSort = function(arr) { | |
var len = arr.length, | |
temp; | |
for (var i = 0; i < len; i++) { | |
for (var j = 0, stop = len - i; j < stop; j++) { | |
if (arr[j] > arr[j + 1]) { | |
temp = arr[j + 1]; | |
arr[j + 1] = arr[j]; | |
arr[j] = temp; | |
} | |
} | |
} | |
return arr; | |
}; | |
//MEDIUM BUBBLE SORT | |
var bubbleSort2 = function(arr) { | |
var len = arr.length, | |
i = 0, | |
temp; | |
while (i < len) { | |
for (var j = 0, stop = len - i; j < stop; j++) { | |
if (arr[j] > arr[j + 1]) { | |
temp = arr[j + 1]; | |
arr[j + 1] = arr[j]; | |
arr[j] = temp; | |
} | |
} | |
i++; | |
} | |
return arr; | |
}; | |
//SLOWEST BUBBLE SORT | |
var bubbleSort3 = function(arr) { | |
var length = arr.length, | |
temp; | |
var sorted = []; | |
while (sorted.length < length) { | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] > arr[i + 1]) { | |
temp = arr[i + 1]; | |
arr[i + 1] = arr[i]; | |
arr[i] = temp; | |
} | |
} | |
var largest = Number(arr.splice(arr.length - 1).join('')); | |
sorted.unshift(largest); | |
} | |
return sorted; | |
}; | |
var bubbleSort4 = function(arr) { | |
var len = arr.length, | |
temp; | |
arr.forEach(function(_, index) { | |
for (var i = 0, stop = len - index; i < stop; i++) { | |
if (arr[i] > arr[i + 1]) { | |
temp = arr[i + 1]; | |
arr[i + 1] = arr[i]; | |
arr[i] = temp; | |
} | |
} | |
}); | |
return arr; | |
}; | |
/*************TEST SORT METHODS*******************/ | |
var range = function(start, stop, step) { | |
var arr = []; | |
while (start <= stop) { | |
arr.push(start); | |
start += step; | |
} | |
return arr; | |
}; | |
Array.prototype.shuffle = function() { | |
var listClone = this.slice(), //clone list | |
rndPos, temp; | |
for (var i = 0; i < listClone.length - 1; i++) { | |
rndPos = Math.floor(Math.random() * listClone.length); | |
temp = listClone[i]; //store i value in temp array; | |
listClone[i] = listClone[rndPos]; //replace i with random value | |
listClone[rndPos] = temp; // replace random with origina i stored in temp; | |
} | |
return listClone; | |
}; | |
var haystack = range(1, 15, 1).shuffle(); | |
console.log(haystack); | |
console.log('Bubble Sort ForEach: ' + bubbleSort4(haystack)); //Bubble Sort ForEach: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 | |
console.log('Bubble Sort Fast: ' + bubbleSort(haystack)); // Bubble Sort Fast: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 | |
console.log('Bubble Sort Medium: ' + bubbleSort2(haystack)); //Bubble Sort Medium: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 | |
console.log('Bubble Sort Slow: ' + bubbleSort3(haystack)); //Bubble Sort Slow: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 | |
/*************SELECTION SORT*******************/ | |
var selectionSort = function(arr) { | |
var lowest, | |
len = arr.length, | |
temp; | |
arr.forEach(function(_, index) { | |
lowest = index; //assume index is the lowest, prove wrong | |
for (var i = index + 1; i < len; i++) { | |
if (arr[i] < arr[lowest]) | |
lowest = i; //save new lowest index | |
} | |
//if the lowest isn't in our position, swap it. | |
if (index != lowest) { | |
temp = arr[index]; | |
arr[index] = arr[lowest]; | |
arr[lowest] = temp; | |
} | |
}); | |
return arr; | |
}; | |
console.log(selectionSort([9, 5, 10, 1, 4, 2, 12, 14, 13, 3, 6, 8, 11, 15, 7])); | |
//[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment