1function bubbleSort(array) {
2 for (let i = 0; i < array.length; i++) {
3 for (let j = 0; j < array.length; j++) {
4 let item = array[j];
5
6 var nextItem = array[j + 1];
7 if (item > nextItem) {
8 array[j] = nextItem;
9 array[j + 1] = item;
10 }
11 }
12 }
13 return array;
14}
15
16console.log(bubbleSort([9, 5, 7, 1, 0, 2, 4, 10, 1, 6, 3, 5, 8]));
17console.log(bubbleSort([900, 5, 70, 0.1, 0, 02, 4, 100, 1, 6, 35, 56, 8]));
1function bubblesort(array) {
2 len = array.length;
3
4 for (let i = 0; i < len; i++) {
5 for (let j = 0; j < len - i; j++) {
6 let a = array[j];
7 if (a != array[-1]) {
8 var b = array[j + 1];
9 if (a > b) {
10 array[j] = b;
11 array[j + 1] = a;
12 }
13 }
14 }
15 }
16}
17
18let array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
19bubblesort(array);
20console.log(array)
1// like using Array.reverse();
2function bubbleReverse(arr) {
3 for (let i = arr.length - 1; i > 0; i--) {
4 for (let j = arr.length - 1; j > 0; j--) {
5 if (arr[j] > arr[j - 1]) {
6 let temp = arr[j];
7 arr[j] = arr[j - 1];
8 arr[j - 1] = temp;
9 }
10 }
11 }
12 return arr;
13}
14
15// like using Array.sort();
16function bubbleSort(arr) {
17 for (let i = 0; i < arr.length - 1; i++) {
18 for (let j = 0; j < arr.length - 1; j++) {
19 if (arr[j] > arr[j + 1]) {
20 let temp = arr[j];
21 arr[j] = arr[j + 1];
22 arr[j + 1] = temp;
23 }
24 }
25 }
26 return arr;
27}