Created
December 15, 2022 23:10
-
-
Save abdulrahmanAlotaibi/3652bc13aab4b5c90625f6e05c886a97 to your computer and use it in GitHub Desktop.
Bubble Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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