├── README.md └── bubble.go /README.md: -------------------------------------------------------------------------------- 1 | 2 | Go bubble sort example for beginner. 3 | 4 | 1. Clone 5 | 6 | ``` 7 | git clone https://github.com/mhshajib/GoBubbleSort.git 8 | ``` 9 | 10 | 2.Go to project directory 11 | 12 | ``` 13 | cd GoBubbleSort 14 | ``` 15 | 16 | 3. Compile 17 | 18 | ``` 19 | go build bubble.go 20 | ``` 21 | 22 | 4. Run 23 | 24 | ``` 25 | ./bubble 26 | ``` 27 | 28 | Done. :smile: 29 | -------------------------------------------------------------------------------- /bubble.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | a:= [6]int{5, 1, 6, 2, 4, 3} //Setting values to array 8 | 9 | fmt.Println("Array Before Sorting : ", a) 10 | 11 | for i:=0; i<6; i++ { 12 | flag := 0;//Taking Flag Variable 13 | for j:=0; j<6-i-1; j++ { 14 | if a[j] > a[j+1] { 15 | temp := a[j]; 16 | a[j] = a[j+1]; 17 | a[j+1] = temp; 18 | flag = 1;//setting flag as 1, if swapping occurs 19 | } 20 | } 21 | if flag == 0 { 22 | break//breaking out for loop to reduce extra loops 23 | } 24 | } 25 | 26 | fmt.Println("Sorted Array : ", a) 27 | } --------------------------------------------------------------------------------