Skip to content

Commit

Permalink
Implement BubbleSort in Go (Asiatik#360)
Browse files Browse the repository at this point in the history
* Implement BubbleSort in Go

Implement Bubble Sort Algorithm in Go Programming Language

* Update BubbleSort.go
  • Loading branch information
AmrDeveloper authored and tstreamDOTh committed Oct 26, 2018
1 parent fbe380e commit 1e1d47e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Sorting/Bubble Sort/Go/BubbleSort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import "fmt"

func main() {
//Test Code
array := []int{1, 6, 2, 4, 9, 0, 5, 3, 7, 8}
fmt.Println("Unsorted array: ", array)
sort(array)
fmt.Println("Sorted array: ", array)
}

func sort(arr []int) {
for i := 0; i < len(arr); i++ {
for j := 0; j < i; j++ {
if arr[j] > arr[i] {
swap(arr, i, j)
}
}
}
}

func swap(arr []int, x, y int) {
temp := arr[x]
arr[x] = arr[y]
arr[y] = temp
}

0 comments on commit 1e1d47e

Please sign in to comment.