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

Divide and conquer readme text changes #126

Closed
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
21 changes: 13 additions & 8 deletions dnc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,48 @@ import "fmt"

func main() {
list := []int{1, 2, 3, 4, 5}
// Testing the recursive binary search
fmt.Println(binarySearchRecursive(list, 0, len(list), 5)) // Prints 4
// Testing the iterative binary search
fmt.Println(binarySearchIterative(list, 5)) // Prints 4
}

// binarySearchRecursive conducts a recursive binary search in 'list' within 'low' and 'high' bounds for 'target'.
func binarySearchRecursive(list []int, low, high, target int) int {
if low > high {
return -1
return -1 // Base case: target not found
}

mid := (low + high) / 2
mid := (low + high) / 2 // Calculate the middle index

if list[mid] == target {
return mid
return mid // Target found
} else if list[mid] > target {
return binarySearchRecursive(list, low, mid-1, target)
} else {
return binarySearchRecursive(list, mid+1, high, target)
}
}

// binarySearchIterative performs an iterative binary search in 'list' for 'target'.
func binarySearchIterative(list []int, target int) int {
low := 0
high := len(list) - 1

for low <= high {
mid := (low + high) / 2
mid := (low + high) / 2 // Calculate the middle index


if list[mid] < target {
low = mid + 1
low = mid + 1 // Adjust the low boundary
} else if list[mid] > target {
high = mid - 1
high = mid - 1 // Adjust the high boundary
} else {
return mid
return mid // Target found
}
}

return -1
return -1 // Target not found
}
```

Expand Down