Bubble sort, Select sort, Insertion sort
let arr = [2, 4, 6, 5, 1, 3];
function bubbleSort(arr) {
for (let i = arr.length - 1; i > 0; i--) {
for (let j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
console.log(bubbleSort(arr));
let arr = [2, 4, 6, 5, 1, 3];
function selectionSort(arr) {
let min;
for (let i = 0; i < arr.length - 1; i++) {
min = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[min] > arr[j]) min = j;
}
if (i !== min) {
let temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
return arr;
}
console.log(selectionSort(arr));
function insertionSort(arr) {
let temp;
for (let i = 1; i < arr.length; i++) {
temp = arr[i];
for(var j = i - 1; arr[j] > temp && j > -1; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
return arr;
}