Skip to content

Instantly share code, notes, and snippets.

@lrvick
Created October 17, 2013 20:18
Show Gist options
  • Save lrvick/7031534 to your computer and use it in GitHub Desktop.
Save lrvick/7031534 to your computer and use it in GitHub Desktop.
Comparison of integer sorting algorithms in JavaScript
function jsSort(items){
var steps = 0
items.sort(function(a,b){
steps = steps+1
return a-b
})
return [items,steps]
}
// I have no idea what kind of sort this is but it is faster than bubble
function lanceSort(items){
var sortedItems = [0]
var index = 0
var steps = 0
while (index != items.length){
for (i=0; i < sortedItems.length; i++){
steps = steps+1
if ((items[index] < sortedItems[i]) || (sortedItems.length == i + 1)){
sortedItems.splice(i,0,items[index])
index = index +1;
break;
}
}
}
sortedItems.pop()
return [sortedItems,steps]
}
function bubbleSort(items){
var sorted = false
var steps = 0
while (sorted == false){
var change = false
for (var i=0; i < items.length; i++){
steps = steps+1
if (items[i] > items[i+1]){
var a = items[i]
items[i] = items[i+1]
items[i+1] = a
change = true
}
}
sorted = !change
}
return [items,steps]
}
function quickSort(items) {
var steps = 0;
var partition = function(items, left, right) {
var pivot = items[Math.floor((right + left) / 2)],
i = left,
j = right;
while (i <= j) {
while (items[i] < pivot) {
i++;
}
while (items[j] > pivot) {
j--;
}
if (i <= j) {
swap(items, i, j);
i++;
j--;
}
}
return i;
}
var swap = function(items, firstIndex, secondIndex){
var temp = items[firstIndex];
items[firstIndex] = items[secondIndex];
items[secondIndex] = temp;
}
var sort = function(items,left,right){
steps = steps + 1
var index;
if (items.length > 1) {
left = typeof left != "number" ? 0 : left;
right = typeof right != "number" ? items.length - 1 : right;
index = partition(items, left, right);
if (left < index - 1) {
sort(items, left, index - 1);
}
if (index < right) {
sort(items, index, right);
}
}
return [items,steps];
}
return sort(items)
}
function randArray(n){
var arr = [];
for (var i = 0, l = n; i < l; i++) {
arr.push(Math.round(Math.random() * l))
}
return arr
}
function averageSteps(fn,numRuns,inputSize){
var steps = []
var stepsSum = 0
var averageSteps
for (var i=0;i<numRuns;i++){
var result = fn(randArray(inputSize))
steps.push(result[1])
}
for (var i=0; i < steps.length; i++){
stepsSum = stepsSum + steps[i]
}
averageSteps = stepsSum / steps.length
console.log('Average steps for '+numRuns+' runs with an input of size '+inputSize+' is '+ averageSteps)
return averageSteps
}
console.log('\nJavaScript Built-In sort:')
averageSteps(jsSort,10,10)
averageSteps(jsSort,100,100)
averageSteps(jsSort,1000,1000)
console.log('\nLance sort:')
averageSteps(lanceSort,10,10)
averageSteps(lanceSort,100,100)
averageSteps(lanceSort,1000,1000)
console.log('\nBubble Sort:')
averageSteps(bubbleSort,10,10)
averageSteps(bubbleSort,100,100)
averageSteps(bubbleSort,1000,1000)
console.log('\nQuick Sort:')
averageSteps(quickSort,10,10)
averageSteps(quickSort,100,100)
averageSteps(quickSort,1000,1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment