diff --git a/Golang/bubbleSort Program.go b/Golang/bubbleSort Program.go new file mode 100644 index 0000000..cbdad2f --- /dev/null +++ b/Golang/bubbleSort Program.go @@ -0,0 +1,36 @@ +package main + +import "fmt" + +func bubbleSort(arr []int) []int{ + + for i:=0; i<=len(arr)-1; i++{ + for j:=0; j arr[j+1]{ + arr[j], arr[j+1] = arr[j+1], arr[j] + } + } + } + return arr +} + +func main(){ + fmt.Println(bubbleSort([]int{9, 2, 3, 1, 0})) + fmt.Println(bubbleSort([]int{17, 3, 1, 8, 9})) + fmt.Println(bubbleSort([]int{8, 2, 7, 14, 5})) +} + +/* +Bubble Sort Program +-------------------- +Input array - {9, 2, 3, 1, 0} + {17, 3, 1, 8, 9} + {8, 2, 7, 14, 5} + +Output Sorted array - [0 1 2 3 9] + [1 3 8 9 17] + [2 5 7 8 14] + +Time Complexity - O(n2) + +*/ \ No newline at end of file diff --git a/Golang/selectionSort Program.go b/Golang/selectionSort Program.go new file mode 100644 index 0000000..4bb6d9a --- /dev/null +++ b/Golang/selectionSort Program.go @@ -0,0 +1,38 @@ +package main + +import "fmt" + +func selectionSort(items []int) []int{ + var n = len(items) + for i := 0; i < n; i++ { + var minIdx = i + for j := i; j < n; j++ { + if items[j] < items[minIdx] { + minIdx = j + } + } + items[i], items[minIdx] = items[minIdx], items[i] + } + return items +} + +func main(){ + fmt.Println(selectionSort([]int{9, 2, 3, 1, 0})) + fmt.Println(selectionSort([]int{17, 3, 1, 8, 9})) + fmt.Println(selectionSort([]int{8, 2, 7, 14, 5})) +} + +/* +Selection Sort Program +-------------------- +Input array - {9, 2, 3, 1, 0} + {17, 3, 1, 8, 9} + {8, 2, 7, 14, 5} + +Output Sorted array - [0 1 2 3 9] + [1 3 8 9 17] + [2 5 7 8 14] + +Time Complexity - O(n2) + +*/ \ No newline at end of file