We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fd48226 commit 7668a1fCopy full SHA for 7668a1f
array/bubble_sort.go
@@ -2,10 +2,13 @@ package array
2
3
// BubbleSort solves the problem in O(n^2) time and O(1) space.
4
func BubbleSort(input []int) {
5
- for i := range input {
6
- for j := range input {
7
- if input[i] < input[j] {
8
- input[i], input[j] = input[j], input[i]
+ swapped := true
+ for swapped {
+ swapped = false
+ for i := 1; i < len(input); i++ {
9
+ if input[i] < input[i-1] {
10
+ input[i], input[i-1] = input[i-1], input[i]
11
+ swapped = true
12
}
13
14
0 commit comments