Javatpoint Logo
Javatpoint Logo

What are linear search and binary search in data structure?

Linear search and binary search are both methods used to search an element. We have given both of these methods an array and a key-value; all we need to do is search that key in the array. We will return the index value corresponding to that key value if we find it. If we are not able to find that key in the array, then we will print the message that the value is not found.

  • We must sort the array in linear search, neither ascending nor descending order.
  • In binary search, before we apply the procedure of binary search on the array, first we are required to sort the array either in linear search or.
  • We will traverse the array linearly in linear search and compare each array value with the key linearly, sequential.
  • In binary search, we will apply the divide and conquer approach; first, we will find the mid element and then divide the array into two parts. At a time, we will work on one part to get the desired result.
  • The binary search is a more efficient method than the linear search approach.
  • If the given array is sorted, we should prefer the binary search approach over the linear search.
  • But if the array is given is not in the sorted order, then the binary search approach is not an efficient method over a linear search.

Let us discuss both linear search and binary search in more detail.

Linear search in data structure

Objective: Linear search is used to search any element present in the array. In linear search, we don't need a sorted array. Any random array can be used to find the element. We have provided an array and a key-value; key-value is nothing but the value needed to be searched in the given array. If the array is found successfully in the array, we need to return the corresponding index of that value. The reason for returning the index is that we return the location of that key-value or element present in that array. And if in case the value is not found in the array, then we need to return the simple statement that the value is not present in the array.

Algorithm to implement Linear search to search an element -

Step 1 - Take an array from the user of ' n ' elements; elements refer to the non-negative integers in the main function. Also, take the key value from the user to generate the result accordingly.

Step 2 - Make a function call to the Linear search function to find the corresponding index of that key value from the array. Pass the original array, number of elements, and pass the key value, array, and the size of the array in the form of argument in that function.

Step 3 - We can implement the linear search function using two methods; one is by using simple loop iterations, and another one is the recursive approach.

Step 4 - Using the iterative approach, we will use the loop, and it depends on the programmer that they will use which type of loop, whether it is for loop, while loop, or do-while loop.

Step 5 - The basic criteria of implementing the loop is, running the loop from 0 to size - 1 of an array; inside the body of the loop, we will check the condition that whether the value of the key is matched with the index I, if matches then we will break the loop and return the value of I, which denotes the index of the array, else we will increment the value of i by 1 and repeat the loop until we get the key or the array is over.

Step 6 - If we are using the recursive approach, we will write the base condition in the function such that if the value present at the last index matches with the key-value, we will return the index. Otherwise, we will decrement the size of the array and again recursively call the same linear search function.

Step 7 - If the value does not match an array element, we will return the - 1.

Step 8 - The - 1 will return in the main function, and again in the main function, we will check the condition such that if the return value is equal to - 1, then we will print the message that the key does not found in the array. And if the value is not equal to - 1, then we will simply print the message that the key is found at particular index i.

Step 9 - Finally, we will get the desired output, as a result, on the screen.

Implementation of the Linear search in C programming language using Iterative approach

The output of the above program

Enter the size of an array: 10
Enter Array
 12
13
15
8
7
9
6
11
10
4
 Enter the value to search: 7
 Value present at index: 4

Implementation of the Linear search in C++ programming language using Iterative approach

The output of the above program

Enter the size of an array: 10
Enter Array
 12
13
15
8
7
9
6
11
10
4
 Enter the value to search: 7
 Value present at index: 4

Implementation of the Linear search in Java programming language using Iterative approach

The output of the above program

Enter the size of an array: 10
Enter Array
 12
13
15
8
7
9
6
11
10
4
 Enter the value to search: 7
 Value present at index: 4

Implementation of the Linear search in C programming language using Recursive approach

The output of the above program

Enter the size of an array: 10
Enter Array
 12
13
15
8
7
9
6
11
10
4
 Enter the value to search: 7
 Value present at index: 4

Implementation of the Linear search in C++ programming language using Recursive approach

The output of the above program

Enter the size of an array: 10
Enter Array
 12
13
15
8
7
9
6
11
10
4
 Enter the value to search: 7
 Value present at index: 4

Implementation of the Linear search in Java programming language using Recursive approach

The output of the above program

Enter the size of an array: 10
Enter Array
 12
13
15
8
7
9
6
11
10
4
 Enter the value to search: 7
 Value present at index: 4

Binary search in data structure

Objective: Binary search is used to search any element present in the array. In Binary search, we must have a sorted array, and any random array can not be used to find the element from it. We have provided an array and a key-value; key-value is nothing but the value needed to be searched in the given array. If the array is found successfully in the array, we need to return the corresponding index of that value. The reason for returning the index is that we return the location of that key-value or element present in that array. And if in case the value is not found in the array, then we need to return the simple statement that the value is not present in the array. If the array provided by the user is not sorted, then first, we are required to sort the array, and then we apply the searching procedure over it.

Algorithm to implement Binary search to search an element -

Step 1 - Take an array from the user of ' n ' elements; elements refer to the non-negative integers in the main function. Also, take the key value from the user to generate the result accordingly.

Step 2 - Make a function call to the Binary search function to find the corresponding index of that key value from the array. Pass the original array, the first index of the sorted array, the last index of the sorted array, and pass the key value and the size of the array in the form of argument in that function.

Step 3 - We can implement the binary search function using two methods; one is by using simple loop iterations, and another one is the recursive approach.

Step 4 - Using the iterative approach, we will use the loop, and it depends on the programmer that they will use which type of loop, whether it is for loop, while loop, or do-while loop.

Step 5 - If we are using the recursive approach, then we will instead of using the loop, will simply write the base condition in the function such that if the value present at the last index matches with the key-value, then we will return the index, otherwise we will decrement the size of the array and again recursively call the same linear search function.

Step 6 - The basic criteria of implementing the loop is, running the loop from 0 to size - 1 of an array; inside the body of the loop, we will check the condition that whether the value of the key is matched with the mid element, if matches then we will break the loop and return the value of mid, which denotes the index of the array, else we will simply increment the value of i by 1 and repeat the loop until we get the key or the array is over.

Step 7 - If the mid element is greater than the key-value, then we will do partition the array and call for the array starting from index 0 and last index at mid - 1, then the remaining work will be done on the other half, and the same process will be repeated for the same.

Step 8 - If the key-value exceeds the mid element, we will again partition the array and call for the array starting from index mid + 1 to n. In all the above cases, if any of the cases match the key value with the mid-value, we will return the mid-value, which indicates the index of the key value.

Step 9 - If the value does not match an array element, we will return the - 1.

Step 10 - The - 1 will return in the main function, and again in the main function, we will check the condition such that if the return value is equal to - 1, then we will print the message that the key does not found in the array. And if the value is not equal to - 1, then we will simply print the message that the key is found at particular index i.

Step 11 - Finally, we will get the desired output, as a result, on the screen.

Implementation of the Binary search in C programming language using Iterative approach

The output of the above program

Enter the size of an array: 10
Enter Sorted Array
 12
13
15
18
20
22
26
30
40
44
 Enter the value to search: 20
 Value present at index: 4

Implementation of the Binary search in C++ programming language using Iterative approach

The output of the above program

Enter the size of an array: 10
Enter Sorted Array
 12
13
15
18
20
22
26
30
40
44
 Enter the value to search: 20
 Value present at index: 4

Implementation of the Binary search in Java programming language using Iterative approach

The output of the above program

Enter the size of an array: 10
Enter Sorted Array
 12
13
15
18
20
22
26
30
40
44
 Enter the value to search: 20
 Value present at index: 4

Implementation of the Binary search in C programming language using Recursive approach

The output of the above program

Enter the size of an array: 10
Enter Sorted Array
 12
13
15
18
20
22
26
30
40
44
 Enter the value to search: 20
 Value present at index: 4

Implementation of the Binary search in C++ programming language using Recursive approach

The output of the above program

Enter the size of an array: 10
Enter Sorted Array
 12
13
15
18
20
22
26
30
40
44
 Enter the value to search: 20
 Value present at index: 4

Implementation of the Binary search in Java programming language using Recursive approach

The output of the above program

Enter the size of an array: 10
Enter Sorted Array
 12
13
15
18
20
22
26
30
40
44
 Enter the value to search: 20
 Value present at index: 4






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA