diff --git a/Algorithms/Searching Algorithms/Go/binarySearch_usingRecursion.go b/Algorithms/Searching Algorithms/Go/binarySearch_usingRecursion.go new file mode 100644 index 0000000..704a283 --- /dev/null +++ b/Algorithms/Searching Algorithms/Go/binarySearch_usingRecursion.go @@ -0,0 +1,47 @@ +package main + +import "fmt" + +// Recursive Binary Search Implementation +func binarySearch(search int, startIndex int, endIndex int, array []int) int { + + // starting index should be less than or equal to end index + if startIndex <= endIndex { + + // getting the middle element of subarray + middle := (startIndex + endIndex) / 2 + + // check if middle element is the searching element or not + // if middle element is greater than the searching element then search in left- + // -side of middle element or else search in right side of middle elements + if array[middle] == search { + return middle + } else if array[middle] > search { + return binarySearch(search, startIndex, middle-1, array) + } else { + return binarySearch(search, middle+1, endIndex, array) + } + + } + + // return -1 if element not found + return -1 +} + +// Main Function +func main() { + + // array should be sorted for binary search + array := []int{1, 2, 9, 20, 31, 45, 63, 70, 100} + fmt.Println() + + var res = binarySearch(63, 0, 8, array) + + if res != -1 { + fmt.Print("Element found at index : ") + fmt.Println(res) + } else { + fmt.Println("Element not found") + } + +} diff --git a/README.md b/README.md index eaa9772..1c3dc8d 100644 --- a/README.md +++ b/README.md @@ -78,11 +78,12 @@ Linear Search |✔️|✔️| | ✔️ | | | Binary Search | |✔️| ✔️| ✔️ | | |✔️| #### Recursion -Section/Language | C | C++ | Java | Python | Javascript | Scala | ------------------|----|-----|------|--------|------------|-------| +Section/Language | C | C++ | Java | Python | Javascript | Scala | Go | +-----------------|----|-----|------|--------|------------|-------| -- | Fibonacci |✔️ | | |✔️ | | | Factorial |✔️ | | | ✔️ | | | Tower of Hanoi |✔️ | | | | | | GCD |✔️ | | | | | | LCM | | | | | | Pacal Traingle |✔️ | | | | | +Binary Search | | | | | | | ✔️ |