|
| 1 | +/* |
| 2 | +// Binary Search in an array of a given size |
| 3 | +// Prints if a given number is found in a sorted array and prints the index of the number if it is found |
| 4 | +*/ |
| 5 | + |
| 6 | +package main |
| 7 | + |
| 8 | +import ("fmt" |
| 9 | + //"reflect" |
| 10 | +) |
| 11 | + |
| 12 | +// Binary Search Iterative |
| 13 | + |
| 14 | +func BinarySearchIterative(array *[]int,startIdx int,endIdx int, searchNum int) int { |
| 15 | + arr := *array |
| 16 | + var returnVal int |
| 17 | + fmt.Println("Using the iterative Binary Search approach") |
| 18 | + for (true) { |
| 19 | + mid := startIdx + (endIdx - startIdx)/2 |
| 20 | + if (endIdx<startIdx) { |
| 21 | + //fmt.Println("Element not found") |
| 22 | + returnVal := -1 |
| 23 | + return returnVal |
| 24 | + } else if arr[mid] == searchNum { |
| 25 | + //fmt.Println("Element found at index:",mid) |
| 26 | + returnVal := mid |
| 27 | + return returnVal |
| 28 | + } else if arr[mid] > searchNum { |
| 29 | + endIdx = mid-1 |
| 30 | + } else { |
| 31 | + startIdx = mid+1 |
| 32 | + } |
| 33 | + } |
| 34 | + return returnVal |
| 35 | +} |
| 36 | + |
| 37 | +// Binary Search Recursive algorithm |
| 38 | +func BinarySearchRecursive(array *[]int,startIdx int,endIdx int, searchNum int) int { |
| 39 | + arr := *array |
| 40 | + var returnVal int |
| 41 | + //fmt.Println("Using the recursive Binary Search approach") |
| 42 | + mid := startIdx + (endIdx - startIdx)/2 |
| 43 | + if (endIdx<startIdx) { |
| 44 | + //fmt.Println("Element not found") |
| 45 | + returnVal = -1 |
| 46 | + return returnVal |
| 47 | + } else if (arr[mid] == searchNum) { |
| 48 | + //fmt.Println("Element found at Index:",mid) |
| 49 | + returnVal = mid |
| 50 | + return returnVal |
| 51 | + } else if (arr[mid] > searchNum) { |
| 52 | + returnVal = BinarySearchRecursive(array,startIdx,mid-1,searchNum) |
| 53 | + } else { |
| 54 | + returnVal = BinarySearchRecursive(array,mid+1,endIdx,searchNum) |
| 55 | + } |
| 56 | + return returnVal |
| 57 | +} |
| 58 | + |
| 59 | +// Unit Test for Binary Search |
| 60 | +func main(){ |
| 61 | + |
| 62 | + /* |
| 63 | + var length,searchNum int |
| 64 | + fmt.Println("Enter the size of the array") |
| 65 | + fmt.Scanf("%d",&length) |
| 66 | + fmt.Println("Enter the ascending order of array elements seperated by a space") |
| 67 | + arr := make([]int,length) |
| 68 | + for i:=0;i<length;i++ { |
| 69 | + fmt.Scanln(&arr[i]) |
| 70 | + } |
| 71 | + fmt.Println("Enter the number to search in the array") |
| 72 | + fmt.Scanf("%d",&searchNum) |
| 73 | + fmt.Println("Array:",arr) |
| 74 | + */ |
| 75 | + |
| 76 | + arr := []int{1,10,20,47,59,63,75,88,99,107,120,133,155,162,176,188,199,200,210,222} |
| 77 | + searchNum := 155 //Solution is 12 |
| 78 | + //search Num := 75 //Solution is 6 |
| 79 | + |
| 80 | + index1 := BinarySearchIterative(&arr,0,len(arr)-1,searchNum) |
| 81 | + fmt.Println("Number found at index:",index1) |
| 82 | + fmt.Println("Using the recursive Binary Search approach") |
| 83 | + index1 = BinarySearchRecursive(&arr,0,len(arr)-1,searchNum) |
| 84 | + fmt.Println("Number found at index:",index1) |
| 85 | + |
| 86 | +} |
0 commit comments