Skip to content

Instantly share code, notes, and snippets.

@vamshisuram
Created December 27, 2014 14:32
Show Gist options
  • Save vamshisuram/38aed0ac2131ca995757 to your computer and use it in GitHub Desktop.
Save vamshisuram/38aed0ac2131ca995757 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// sorting - trial 2
var a = [];
var i, j, k, item, f_item;
for (i=0; i<20; i++) {
item = Math.floor(Math.random() * 100 + 1);
a.push(item);
}
console.log(a);
function swaps() {
for (j=0; j<19; j++) {
// swap j j+1
if (a[j] > a[j+1]) {
f_item = a[j];
a[j] = a[j+1];
a[j+1] = f_item;
}
}
}
for (k=0; k<19; k++) {
swaps();
}
console.log(a);
</script>
<script id="jsbin-source-javascript" type="text/javascript">// sorting - trial 2
var a = [];
var i, j, k, item, f_item;
for (i=0; i<20; i++) {
item = Math.floor(Math.random() * 100 + 1);
a.push(item);
}
console.log(a);
function swaps() {
for (j=0; j<19; j++) {
// swap j j+1
if (a[j] > a[j+1]) {
f_item = a[j];
a[j] = a[j+1];
a[j+1] = f_item;
}
}
}
for (k=0; k<19; k++) {
swaps();
}
console.log(a);
</script></body>
</html>
// sorting - trial 2
var a = [];
var i, j, k, item, f_item;
for (i=0; i<20; i++) {
item = Math.floor(Math.random() * 100 + 1);
a.push(item);
}
console.log(a);
function swaps() {
for (j=0; j<19; j++) {
// swap j j+1
if (a[j] > a[j+1]) {
f_item = a[j];
a[j] = a[j+1];
a[j+1] = f_item;
}
}
}
for (k=0; k<19; k++) {
swaps();
}
console.log(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment