|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +func main() { |
| 8 | + // Example sorted array for testing |
| 9 | + arr := []int{1, 3, 5, 7, 9, 11, 13, 15, 17, 19} |
| 10 | + |
| 11 | + // Test binary search |
| 12 | + target := 7 |
| 13 | + index := BinarySearch(arr, target) |
| 14 | + fmt.Printf("BinarySearch: %d found at index %d\n", target, index) |
| 15 | + |
| 16 | + // Test recursive binary search |
| 17 | + recursiveIndex := BinarySearchRecursive(arr, target, 0, len(arr)-1) |
| 18 | + fmt.Printf("BinarySearchRecursive: %d found at index %d\n", target, recursiveIndex) |
| 19 | + |
| 20 | + // Test find insert position |
| 21 | + insertTarget := 8 |
| 22 | + insertPos := FindInsertPosition(arr, insertTarget) |
| 23 | + fmt.Printf("FindInsertPosition: %d should be inserted at index %d\n", insertTarget, insertPos) |
| 24 | +} |
| 25 | + |
| 26 | +// BinarySearch performs a standard binary search to find the target in the sorted array. |
| 27 | +// Returns the index of the target if found, or -1 if not found. |
| 28 | +func BinarySearch(arr []int, target int) int { |
| 29 | + // TODO: Implement this function |
| 30 | + if len(arr) == 0 || target > arr[len(arr)-1] || target < arr[0] { |
| 31 | + return -1 |
| 32 | + } |
| 33 | + |
| 34 | + l := 0 |
| 35 | + r := len(arr) - 1 |
| 36 | + |
| 37 | + for l <= r { |
| 38 | + mid := l + (r-l)/2 |
| 39 | + |
| 40 | + if target == arr[mid] { |
| 41 | + return mid |
| 42 | + } |
| 43 | + if target < arr[mid] { |
| 44 | + r = mid - 1 |
| 45 | + } else { |
| 46 | + l = mid + 1 |
| 47 | + } |
| 48 | + } |
| 49 | + return -1 |
| 50 | +} |
| 51 | + |
| 52 | +// BinarySearchRecursive performs binary search using recursion. |
| 53 | +// Returns the index of the target if found, or -1 if not found. |
| 54 | +func BinarySearchRecursive(arr []int, target int, left int, right int) int { |
| 55 | + // TODO: Implement this function |
| 56 | + if left > right { |
| 57 | + return -1 |
| 58 | + } |
| 59 | + |
| 60 | + mid := (left + right) / 2 |
| 61 | + |
| 62 | + if target == arr[mid] { |
| 63 | + return mid |
| 64 | + } else if target < arr[mid] { |
| 65 | + return BinarySearchRecursive(arr, target, left, mid-1) |
| 66 | + } else { |
| 67 | + return BinarySearchRecursive(arr, target, mid+1, right) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// FindInsertPosition returns the index where the target should be inserted |
| 72 | +// to maintain the sorted order of the array. |
| 73 | +func FindInsertPosition(arr []int, target int) int { |
| 74 | + // TODO: Implement this function |
| 75 | + if len(arr) == 0 { |
| 76 | + return 0 |
| 77 | + } |
| 78 | + |
| 79 | + l := 0 |
| 80 | + r := len(arr) - 1 |
| 81 | + |
| 82 | + for l <= r { |
| 83 | + mid := l + (r-l)/2 |
| 84 | + |
| 85 | + if target == arr[mid] { |
| 86 | + return mid |
| 87 | + } else if target < arr[mid] { |
| 88 | + r = mid - 1 |
| 89 | + } else { |
| 90 | + l = mid + 1 |
| 91 | + } |
| 92 | + } |
| 93 | + return l |
| 94 | +} |
0 commit comments