1bubbleSort(Array) {
2 let len = Array.length;
3 for (let i = 0; i < len; i++) { //you can also use "for in", so you don't need the variable "len"
4 for (let j = 0; j < len; j++) {
5 if (Array[j] > Array[j + 1]) {
6 let tmp = Array[j];
7 Array[j] = Array[j + 1];
8 Array[j + 1] = tmp;
9 }
10 }
11 }
12 return Array;
13};
1function BubbleSort(arr) {
2 const sortedArray = Array.from(arr);
3 let swap;
4 do {
5 swap = false;
6 for (let i = 1; i < sortedArray.length; ++i) {
7 if (sortedArray[i - 1] > sortedArray[i]) {
8 [sortedArray[i], sortedArray[i - 1]] = [sortedArray[i - 1], sortedArray[i]];
9 swap = true;
10 }
11 }
12 } while (swap)
13 return sortedArray;
14 }
15
16 console.log(BubbleSort([7,99,1,88,34,2,90,7]));