const bubbleSort = list => { for (let i = 0; i < list.length - 1; i++) { let isExchagne = false; for (let j = 0; j < list.length - i - 1; j++) { if (list[j] > list[j + 1]) { isExchagne = true; [list[j], list[j + 1]] = [list[j + 1], list[j]]; } } if (!isExchagne) { break; } } return list; } const arr = [1, 2, 3, 4]; console.log(bubbleSort(arr));