Q:
Golang program to search an item in the array using binary search
belongs to collection: Golang Array Programs
Golang Array Programs
- Golang program to demonstrate the example of an Array
- Golang program to demonstrate the shorthand declaration of an Array
- Golang program to create an array without specifying its size
- Golang program to get the size of the array
- Golang program to print the array elements without using the loop
- Golang program to create an array with the existing array
- Golang program to compare two arrays using equal to (==) operator
- Golang program to calculate the sum of all array elements
- Golang program to find the largest elements from the array
- Golang program to find the second largest elements from the array
- Golang program to print even numbers of the array
- Golang program to merge two arrays into third array
- Golang program to reverse an integer array
- Golang program to print the prime numbers from an integer array
- Golang program to search an item in the array using linear search
- Golang program to search an item in the array using binary search
- Golang program to search an item in the array using interpolation search
- Golang program to sort an integer array in ascending order using bubble sort
- Golang program to sort an integer array in descending order using bubble sort
- Golang program to sort an integer array in ascending order using insertion sort
- Golang program to sort an integer array in descending order using insertion sort
- Golang program to sort an integer array in ascending order using selection sort
- Golang program to sort an integer array in descending order using selection sort
- Golang program to add two integer arrays
- Golang program to swap adjacent elements of a one-dimensional array
- Golang program to find the occurrence of an item in the array
- Golang program to find the first repeated element in the array
- Golang program to delete a given item from the array
- Golang program to insert an item in the array
- Golang program to read and print matrix using the two-dimensional matrix
- Golang program to calculate the sum of matrix elements
- Golang program to calculate the sum of rows elements of the matrix
- Golang program to calculate the sum of columns elements of matrix elements
- Golang program to print the Transpose of a matrix
- Golang program to print the left diagonal of the matrix
- Golang program to print the right diagonal of the matrix
- Golang program to print the sum of left diagonal elements of the matrix
- Golang program to print the sum of right diagonal elements of the matrix
- Golang program to add two matrices
- Golang program to multiply two matrices
- Golang program to demonstrate the multidimensional array
Program/Source Code:
The source code to search an item in the array using binary search is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.
In the main() function, we created an array arr and read elements from the user. Then we searched the item in an array using binary search.
In the binary search, we searched the item by dividing the list and find the middle item, but the binary search can be used in a sorted array only.
If the item is found in the array then print the index of an item otherwise print the "Item not found in array" message on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer