Created
January 24, 2021 08:46
-
-
Save tawhidulIKhan/2c67ddec938fc8750578a448eced1d5c to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/ricalef
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
<!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"> | |
const bubbleSort = (arr) => { | |
// loop through left to right | |
// loop again inside from next | |
// compare item with next item | |
// if item is greater than next item then swap position | |
let n = arr.length; | |
for(let i = 0; i < n; i++){ | |
let nextItem = i + 1 | |
for(let j = nextItem; j < arr.length; j++){ | |
if(arr[i] > arr[j]){ | |
let tmp = arr[i] | |
arr[i] = arr[j] | |
arr[j] = tmp | |
} | |
} | |
} | |
return arr | |
} | |
const bubbleSortRecursive = (arr, n) => { | |
if(n === arr.length) return arr; | |
for(let i = 0; i < arr.length; i++){ | |
if(arr[i] > arr[n]){ | |
let tmp = arr[i] | |
arr[i] = arr[n] | |
arr[n] = tmp | |
} | |
} | |
return bubbleSortRecursive(arr, n+1) | |
} | |
let arr = [1,123,4,3223,123,121,1,23,55,778,89,90,2134235,4576]; | |
console.log(bubbleSortRecursive(arr, 0)) | |
// console.log(bubbleSort(arr)) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const bubbleSort = (arr) => { | |
// loop through left to right | |
// loop again inside from next | |
// compare item with next item | |
// if item is greater than next item then swap position | |
let n = arr.length; | |
for(let i = 0; i < n; i++){ | |
let nextItem = i + 1 | |
for(let j = nextItem; j < arr.length; j++){ | |
if(arr[i] > arr[j]){ | |
let tmp = arr[i] | |
arr[i] = arr[j] | |
arr[j] = tmp | |
} | |
} | |
} | |
return arr | |
} | |
const bubbleSortRecursive = (arr, n) => { | |
if(n === arr.length) return arr; | |
for(let i = 0; i < arr.length; i++){ | |
if(arr[i] > arr[n]){ | |
let tmp = arr[i] | |
arr[i] = arr[n] | |
arr[n] = tmp | |
} | |
} | |
return bubbleSortRecursive(arr, n+1) | |
} | |
let arr = [1,123,4,3223,123,121,1,23,55,778,89,90,2134235,4576]; | |
console.log(bubbleSortRecursive(arr, 0)) | |
// console.log(bubbleSort(arr))</script></body> | |
</html> |
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
const bubbleSort = (arr) => { | |
// loop through left to right | |
// loop again inside from next | |
// compare item with next item | |
// if item is greater than next item then swap position | |
let n = arr.length; | |
for(let i = 0; i < n; i++){ | |
let nextItem = i + 1 | |
for(let j = nextItem; j < arr.length; j++){ | |
if(arr[i] > arr[j]){ | |
let tmp = arr[i] | |
arr[i] = arr[j] | |
arr[j] = tmp | |
} | |
} | |
} | |
return arr | |
} | |
const bubbleSortRecursive = (arr, n) => { | |
if(n === arr.length) return arr; | |
for(let i = 0; i < arr.length; i++){ | |
if(arr[i] > arr[n]){ | |
let tmp = arr[i] | |
arr[i] = arr[n] | |
arr[n] = tmp | |
} | |
} | |
return bubbleSortRecursive(arr, n+1) | |
} | |
let arr = [1,123,4,3223,123,121,1,23,55,778,89,90,2134235,4576]; | |
console.log(bubbleSortRecursive(arr, 0)) | |
// console.log(bubbleSort(arr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment