code to write bubblesort in javascript code example
Example 1: bubble sort javascript
bubbleSort(Array) {
let len = Array.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len; j++) {
if (Array[j] > Array[j + 1]) {
let tmp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = tmp;
}
}
}
return Array;
};
Example 2: javascript bubleshort example
function bubbleReverse(arr) {
for (let i = arr.length - 1; i > 0; i--) {
for (let j = arr.length - 1; j > 0; j--) {
if (arr[j] > arr[j - 1]) {
let temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
return arr;
}
function bubbleSort(arr) {
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}