Skip to content

Instantly share code, notes, and snippets.

@melissamcewen
Created August 1, 2018 02:46
Show Gist options
  • Save melissamcewen/e859b297beb73878adbaf41d0c2442d3 to your computer and use it in GitHub Desktop.
Save melissamcewen/e859b297beb73878adbaf41d0c2442d3 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/qoradofete
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function bubbleSort(array) {
var done = false;
while (!done) {
done = true;
for (var i = 1; i < array.length; i += 1) {
console.log("i is " + i + " and value is " + array[i]);
console.log("we are seeing if the previous value " + array[i - 1] + " is bigger than " + array[i])
if (array[i - 1] > array[i]) {
console.log("Ok it is bigger so we'll move our current value one spot forward ")
done = false;
var tmp = array[i - 1];
array[i - 1] = array[i];
console.log("we put the current value " + array[i] + " in position " + (i-1) )
array[i] = tmp;
console.log("we put the previous value " + tmp + " in position " + i);
} else {
console.log("it isn't so we do nothing")
}
console.log("array is now " + array);
}
}
return array;
}
var numbers = [12, 10, 15, 11, 14, 13, 16];
bubbleSort(numbers);
</script>
<script id="jsbin-source-javascript" type="text/javascript">function bubbleSort(array) {
var done = false;
while (!done) {
done = true;
for (var i = 1; i < array.length; i += 1) {
console.log("i is " + i + " and value is " + array[i]);
console.log("we are seeing if the previous value " + array[i - 1] + " is bigger than " + array[i])
if (array[i - 1] > array[i]) {
console.log("Ok it is bigger so we'll move our current value one spot forward ")
done = false;
var tmp = array[i - 1];
array[i - 1] = array[i];
console.log("we put the current value " + array[i] + " in position " + (i-1) )
array[i] = tmp;
console.log("we put the previous value " + tmp + " in position " + i);
} else {
console.log("it isn't so we do nothing")
}
console.log("array is now " + array);
}
}
return array;
}
var numbers = [12, 10, 15, 11, 14, 13, 16];
bubbleSort(numbers);</script></body>
</html>
function bubbleSort(array) {
var done = false;
while (!done) {
done = true;
for (var i = 1; i < array.length; i += 1) {
console.log("i is " + i + " and value is " + array[i]);
console.log("we are seeing if the previous value " + array[i - 1] + " is bigger than " + array[i])
if (array[i - 1] > array[i]) {
console.log("Ok it is bigger so we'll move our current value one spot forward ")
done = false;
var tmp = array[i - 1];
array[i - 1] = array[i];
console.log("we put the current value " + array[i] + " in position " + (i-1) )
array[i] = tmp;
console.log("we put the previous value " + tmp + " in position " + i);
} else {
console.log("it isn't so we do nothing")
}
console.log("array is now " + array);
}
}
return array;
}
var numbers = [12, 10, 15, 11, 14, 13, 16];
bubbleSort(numbers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment