Created
October 7, 2018 02:20
-
-
Save nageshwar-old/febac0e59a450d9c88e53f92d446bdfb to your computer and use it in GitHub Desktop.
JS Bin JavaScript Bubble Sort Algorithm // source https://jsbin.com/revorur
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="JavaScript Bubble Sort Algorithm"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function createArray(size) { | |
return Array.from({length: size}).map((item, index) => Math.floor(Math.random() * size)); | |
} | |
function swap(array, i, j) { | |
let temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
return array; | |
} | |
function bubble_sort_basic(array) { | |
let countOuter = 0; | |
let countInner = 0; | |
let countSwap = 0; | |
for(let i=0; i< array.length; i++) { | |
countOuter++; | |
for(let j=1; j< array.length; j++) { | |
countInner++; | |
if (array[j-1] > array[j]) { | |
countSwap++; | |
swap(array, j-1, j); | |
} | |
} | |
} | |
console.log('outer:', countOuter,'inner:', countInner,'swap:', countSwap); | |
return array; | |
} | |
let before_bubble_sort_basic = createArray(10); | |
console.log(before_bubble_sort_basic); | |
let after_bubble_sort_basic = bubble_sort_basic(before_bubble_sort_basic); | |
console.log(after_bubble_sort_basic); | |
function bubble_sort(array) { | |
let countOuter = 0; | |
let countInner = 0; | |
let countSwap = 0; | |
let swapped; | |
do { | |
swapped = false; | |
countOuter++; | |
for(let i=0; i< array.length; i++) { | |
countInner++; | |
if (array[i] !== undefined && array[i + 1] !== undefined && array[i] > array[i + 1]) { | |
countSwap++; | |
swap(array, i, i+1); | |
swapped = true; | |
} | |
} | |
}while(swapped); | |
console.log('outer:', countOuter,'inner:', countInner,'swap:', countSwap); | |
return array; | |
} | |
let before_bubble_sort = createArray(1000); | |
console.log(before_bubble_sort); | |
let after_bubble_sort = bubble_sort(before_bubble_sort); | |
console.log(after_bubble_sort); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function createArray(size) { | |
return Array.from({length: size}).map((item, index) => Math.floor(Math.random() * size)); | |
} | |
function swap(array, i, j) { | |
let temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
return array; | |
} | |
function bubble_sort_basic(array) { | |
let countOuter = 0; | |
let countInner = 0; | |
let countSwap = 0; | |
for(let i=0; i< array.length; i++) { | |
countOuter++; | |
for(let j=1; j< array.length; j++) { | |
countInner++; | |
if (array[j-1] > array[j]) { | |
countSwap++; | |
swap(array, j-1, j); | |
} | |
} | |
} | |
console.log('outer:', countOuter,'inner:', countInner,'swap:', countSwap); | |
return array; | |
} | |
let before_bubble_sort_basic = createArray(10); | |
console.log(before_bubble_sort_basic); | |
let after_bubble_sort_basic = bubble_sort_basic(before_bubble_sort_basic); | |
console.log(after_bubble_sort_basic); | |
function bubble_sort(array) { | |
let countOuter = 0; | |
let countInner = 0; | |
let countSwap = 0; | |
let swapped; | |
do { | |
swapped = false; | |
countOuter++; | |
for(let i=0; i< array.length; i++) { | |
countInner++; | |
if (array[i] !== undefined && array[i + 1] !== undefined && array[i] > array[i + 1]) { | |
countSwap++; | |
swap(array, i, i+1); | |
swapped = true; | |
} | |
} | |
}while(swapped); | |
console.log('outer:', countOuter,'inner:', countInner,'swap:', countSwap); | |
return array; | |
} | |
let before_bubble_sort = createArray(1000); | |
console.log(before_bubble_sort); | |
let after_bubble_sort = bubble_sort(before_bubble_sort); | |
console.log(after_bubble_sort);</script></body> | |
</html> |
This file contains hidden or 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
function createArray(size) { | |
return Array.from({length: size}).map((item, index) => Math.floor(Math.random() * size)); | |
} | |
function swap(array, i, j) { | |
let temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
return array; | |
} | |
function bubble_sort_basic(array) { | |
let countOuter = 0; | |
let countInner = 0; | |
let countSwap = 0; | |
for(let i=0; i< array.length; i++) { | |
countOuter++; | |
for(let j=1; j< array.length; j++) { | |
countInner++; | |
if (array[j-1] > array[j]) { | |
countSwap++; | |
swap(array, j-1, j); | |
} | |
} | |
} | |
console.log('outer:', countOuter,'inner:', countInner,'swap:', countSwap); | |
return array; | |
} | |
let before_bubble_sort_basic = createArray(10); | |
console.log(before_bubble_sort_basic); | |
let after_bubble_sort_basic = bubble_sort_basic(before_bubble_sort_basic); | |
console.log(after_bubble_sort_basic); | |
function bubble_sort(array) { | |
let countOuter = 0; | |
let countInner = 0; | |
let countSwap = 0; | |
let swapped; | |
do { | |
swapped = false; | |
countOuter++; | |
for(let i=0; i< array.length; i++) { | |
countInner++; | |
if (array[i] !== undefined && array[i + 1] !== undefined && array[i] > array[i + 1]) { | |
countSwap++; | |
swap(array, i, i+1); | |
swapped = true; | |
} | |
} | |
}while(swapped); | |
console.log('outer:', countOuter,'inner:', countInner,'swap:', countSwap); | |
return array; | |
} | |
let before_bubble_sort = createArray(1000); | |
console.log(before_bubble_sort); | |
let after_bubble_sort = bubble_sort(before_bubble_sort); | |
console.log(after_bubble_sort); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment