| 1 | const bubleSort = input => { |
| 2 | let swap; |
| 3 | let n = input.length - 1; |
| 4 | const sortedArray = input.slice(); |
| 5 | do { |
| 6 | swap = false; |
| 7 | for (let index = 0; index < n; index += 1) { |
| 8 | if (sortedArray[index] > sortedArray[index + 1]) { |
| 9 | const tmp = sortedArray[index]; |
| 10 | sortedArray[index] = sortedArray[index + 1]; |
| 11 | sortedArray[index + 1] = tmp; |
| 12 | swap = true; |
| 13 | } |
| 14 | } |
| 15 | n -= 1; |
| 16 | } while (swap); |
| 17 | |
| 18 | return sortedArray; |
| 19 | }; |
| 20 | |
| 21 | export default bubleSort; |