Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary search using recursive method is added #79

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Algorithms/Searching Algorithms/Go/binarySearch_usingRecursion.go
Original file line number Diff line number Diff line change
@@ -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")
}

}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | | | | | | ✔️ |