Created
May 25, 2019 20:28
-
-
Save Nas10ka/8f11f744b58d37a197f1b4f13cac8cce to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/kerafes
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"> | |
// Bubbles sort | |
const sortBubbles = (array) => { | |
let swapped = true; | |
let iterations = 0; | |
while(swapped) { | |
swapped = false; | |
array.forEach((item, index) => { | |
if(item > array[index + 1]) { | |
array[index] = array[index + 1]; | |
array[index + 1] = item; | |
swapped = true; | |
iterations++; | |
} | |
}); | |
} | |
console.log('Bubbles sort: ', array); | |
return array; | |
} | |
// Sort selection | |
const sortSelection = (array) => { | |
const size = array.length; | |
const n = size - 1; | |
for(let i = 0; i < n; i++) { | |
let min = i; | |
for(let j = i + 1; j < size; j++) { | |
if(array[j] < array[min]) min = j; | |
} | |
const t = array[min]; | |
array[min] = array[i]; | |
array[i] = t; | |
} | |
console.log('Sort selection: ', array); | |
return array; | |
} | |
const array = [97, 10, 67, 32, 23, 4]; | |
sortBubbles(array); | |
sortSelection(array); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Bubbles sort | |
const sortBubbles = (array) => { | |
let swapped = true; | |
let iterations = 0; | |
while(swapped) { | |
swapped = false; | |
array.forEach((item, index) => { | |
if(item > array[index + 1]) { | |
array[index] = array[index + 1]; | |
array[index + 1] = item; | |
swapped = true; | |
iterations++; | |
} | |
}); | |
} | |
console.log('Bubbles sort: ', array); | |
return array; | |
} | |
// Sort selection | |
const sortSelection = (array) => { | |
const size = array.length; | |
const n = size - 1; | |
for(let i = 0; i < n; i++) { | |
let min = i; | |
for(let j = i + 1; j < size; j++) { | |
if(array[j] < array[min]) min = j; | |
} | |
const t = array[min]; | |
array[min] = array[i]; | |
array[i] = t; | |
} | |
console.log('Sort selection: ', array); | |
return array; | |
} | |
const array = [97, 10, 67, 32, 23, 4]; | |
sortBubbles(array); | |
sortSelection(array); | |
</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
// Bubbles sort | |
const sortBubbles = (array) => { | |
let swapped = true; | |
let iterations = 0; | |
while(swapped) { | |
swapped = false; | |
array.forEach((item, index) => { | |
if(item > array[index + 1]) { | |
array[index] = array[index + 1]; | |
array[index + 1] = item; | |
swapped = true; | |
iterations++; | |
} | |
}); | |
} | |
console.log('Bubbles sort: ', array); | |
return array; | |
} | |
// Sort selection | |
const sortSelection = (array) => { | |
const size = array.length; | |
const n = size - 1; | |
for(let i = 0; i < n; i++) { | |
let min = i; | |
for(let j = i + 1; j < size; j++) { | |
if(array[j] < array[min]) min = j; | |
} | |
const t = array[min]; | |
array[min] = array[i]; | |
array[i] = t; | |
} | |
console.log('Sort selection: ', array); | |
return array; | |
} | |
const array = [97, 10, 67, 32, 23, 4]; | |
sortBubbles(array); | |
sortSelection(array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment