Skip to content

Commit 417b631

Browse files
Adding bubblesort
1 parent 1e753dc commit 417b631

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Go/sorting/bubble.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package sorting
2+
3+
func bubbleSort(arr []int) int {
4+
swaps := 0
5+
lenArr := len(arr)
6+
7+
for iteration := 0; iteration < lenArr; iteration++ {
8+
for position := 0; position < lenArr-iteration-1; position++ {
9+
if arr[position] > arr[position+1] {
10+
arr[position], arr[position+1] = arr[position+1], arr[position]
11+
swaps++
12+
}
13+
}
14+
}
15+
return swaps
16+
}

0 commit comments

Comments
 (0)