Skip to content

Instantly share code, notes, and snippets.

@abdulrahmanAlotaibi
Created December 15, 2022 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdulrahmanAlotaibi/3652bc13aab4b5c90625f6e05c886a97 to your computer and use it in GitHub Desktop.
Save abdulrahmanAlotaibi/3652bc13aab4b5c90625f6e05c886a97 to your computer and use it in GitHub Desktop.
Bubble Sort
package main
import (
"fmt"
)
func main(){
fmt.Println("Bubble Sort:")
fmt.Println(bubbleSort([]int{5,2,3,22,-4,0,11,-5,1,1}))
fmt.Println(bubbleSort([]int{-5,12,3,2,-4,0,11,-5,1,1}))
fmt.Println(bubbleSort([]int{-5,-2,3,112,-4,0,100,-5,1,2}))
}
func bubbleSort(arr []int ) []int {
for i:= 0 ; i < len(arr) - 1 ; i++ {
for j:=0 ; j < len(arr) - 1 - i ; j++ {
if arr[j] > arr[j + 1] {
arr[j], arr[j+1] = arr[j+1],arr[j]
}
}
}
return arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment