Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// O (N^2)
var theArray = Array.prototype.slice.call(process.argv[2]);
var startTime
var endTime
console.log(theArray, 'unSorted')
function swapValues(one, two) {
var temp = theArray[one]
theArray[one] = theArray[two]
theArray[two] = temp
}
function bubbleSort() {
startTime = new Date().getMilliseconds()
for (var i = theArray.length - 1; i > 1; i--) {
for (var j = 0; j < i; j++) {
if (theArray[j] > theArray[j + 1]) {
swapValues(j, j + 1)
}
}
}
endTime = new Date().getMilliseconds()
console.log('Took ', (endTime - startTime))
}
bubbleSort()
console.log(theArray, 'sorted')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment