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(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}