Created
November 15, 2018 13:04
-
-
Save akinjide/b1af0c046ffb629037cd8d330e9442c3 to your computer and use it in GitHub Desktop.
This file contains 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
// 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