Bubble is a simple generic definition of Bubble sort algorithm.
(arr []T)
| 7 | |
| 8 | // Bubble is a simple generic definition of Bubble sort algorithm. |
| 9 | func Bubble[T constraints.Ordered](arr []T) []T { |
| 10 | swapped := true |
| 11 | for swapped { |
| 12 | swapped = false |
| 13 | for i := 0; i < len(arr)-1; i++ { |
| 14 | if arr[i+1] < arr[i] { |
| 15 | arr[i+1], arr[i] = arr[i], arr[i+1] |
| 16 | swapped = true |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | return arr |
| 21 | } |