Skip to content

Instantly share code, notes, and snippets.

@akinjide
Created November 15, 2018 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akinjide/b1af0c046ffb629037cd8d330e9442c3 to your computer and use it in GitHub Desktop.
Save akinjide/b1af0c046ffb629037cd8d330e9442c3 to your computer and use it in GitHub Desktop.
// 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