From 902493fbe7452064d2ed8fa1c0d95177a31c77bf Mon Sep 17 00:00:00 2001 From: Aryan Singh Date: Thu, 1 Oct 2020 18:31:51 +0530 Subject: [PATCH 1/3] Binary Search Code in Golang --- Searching/go/binary_search.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Searching/go/binary_search.go diff --git a/Searching/go/binary_search.go b/Searching/go/binary_search.go new file mode 100644 index 0000000..69847e8 --- /dev/null +++ b/Searching/go/binary_search.go @@ -0,0 +1,28 @@ +// Binary Search in Golang +package main +import "fmt" + +func binarySearch(target int, array []int) int { + + low := 0 + high := len(array) - 1 + + for low <= high{ + middle:= (high-low) / 2 + low + + if array[middle] == target{ + return middle + } + if array[middle] < target { + low = middle + 1 + }else{ + high = middle - 1 + } + } + return -1 +} + +func main(){ + items := []int{1,2, 4, 8, 16, 32, 64} + fmt.Println(binarySearch(64, items)) +} \ No newline at end of file From 6c581697af278ce987b6c3b214cba75eaf446a42 Mon Sep 17 00:00:00 2001 From: Aryan Singh Date: Thu, 1 Oct 2020 18:42:57 +0530 Subject: [PATCH 2/3] Linear Search Code in Golang --- Searching/go/linear_search.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Searching/go/linear_search.go diff --git a/Searching/go/linear_search.go b/Searching/go/linear_search.go new file mode 100644 index 0000000..e7d46b4 --- /dev/null +++ b/Searching/go/linear_search.go @@ -0,0 +1,18 @@ +// Linear Search in Golang +package main +import "fmt" + +func linearSearch(target int, array []int) int { + + for i:=0; i Date: Thu, 1 Oct 2020 19:38:39 +0530 Subject: [PATCH 3/3] Bubble Sort Code in Golang --- Sorting/bubbleSort/Go/bubble_sort.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Sorting/bubbleSort/Go/bubble_sort.go diff --git a/Sorting/bubbleSort/Go/bubble_sort.go b/Sorting/bubbleSort/Go/bubble_sort.go new file mode 100644 index 0000000..5a06852 --- /dev/null +++ b/Sorting/bubbleSort/Go/bubble_sort.go @@ -0,0 +1,22 @@ +package main + +import "fmt" + +func bubbleSort(array []int) []int { + for i := 0; i < len(array)-1; i++ { + for j := 0; j < len(array)-i-1; j++ { + if array[j] > array[j+1] { + array[j], array[j+1] = array[j+1], array[j] + } + } + } + return array +} + +func main() { + items := []int{8, 6 , 2, -9, 128, 48} + res := bubbleSort(items) + for _, val := range res { + fmt.Println(val) + } +} \ No newline at end of file