Skip to content

Instantly share code, notes, and snippets.

@Nas10ka
Created May 25, 2019 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nas10ka/8f11f744b58d37a197f1b4f13cac8cce to your computer and use it in GitHub Desktop.
Save Nas10ka/8f11f744b58d37a197f1b4f13cac8cce to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/kerafes
<!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>
// 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