Linear & Binary Search
Searching is the process to find a value in the given list of values. In an Array if we have to find an element or value then for that we use some SEARCH ALGORITHMS and two of them are-
⧫Linear Search
⧫Binary Search
Linear Search:
Linear Search which is also known as Sequential Search is used to search an element in an array. In this search-
- First we ask user to give the value for comparison.
- Then the compiler starts comparing the value to the values/elements present in the array.
- Comparison will start from one end to another end.(We can start from any side like from left to right or right to left, it totally depends on the way of writing the code)
- When the given value is founded to be equal to the element in the array then the comparison process gets stopped and you got the element.
- If the compiler checks each and every element in the array and the value is not equal to any of the elements present in the array then element is not found.
for eg: If we have to find the index of an element which has a value "28" in the below array
As there are two element which shares the same values "28", so in this case the compiler gives you the index of the element- " 1 " because it came first (generally we compares from left to right).
Some examples of Linear Search:-
@ Write a code to find the smallest element
- #include <stdio.h>
- int main()
- {
- int array[100], search, c, n;
- printf("Enter number of elements in array\n");
- scanf("%d", &n);
- printf("Enter %d integer(s)\n", n);
- for (c = 0; c < n; c++)
- scanf("%d", &array[c]);
- printf("Enter a number to search\n");
- scanf("%d", &search);
- for (c = 0; c < n; c++)
- {
- if (array[c] == search) /* If required element is found */
- {
- printf("%d is present at location %d.\n", search, c+1);
- break;
- }
- }
- if (c == n)
- printf("%d isn't present in the array.\n", search);
- return 0;
- }
Binary Search:
For the purpose of searching we use Binary Search too. But this search can only be used when the array is Bubble Sort i.e, in ascending order. Let understand it with an example. So in this search-
- First we ask user to give the value for comparison.
Let the entered value is stored in variable "str".
Let the user enters- CASE 1: "20" //first time
CASE 2: "30" //second time
CASE 3: "40" //third time - Then we find the middle value of the array. And to find the middle value we use a formula-
"middle = (highest index - lowest index) / 2" //Highest index = Number of elements - 1
Here middle = (4-0)/2
= 2 - If the middle value is the value user want to search then comparison process gets stopped and you got the element.
Here arr[ middle ]= =str (for CASE 2) - If that is not the value that then the compiler checks whether the given value is greater or lesser than the middle value.
Here for CASE 1: str < arr[ middle ]
CASE 3: str > arr[ middle ]
For CASE 3:
- The index of Lowest Element changes to the index of Middle Element and then it again find out the middle value using same formula.
Here middle = (4+2) / 2 //here lowest index becomes "2"
= 3 - Now the compiler checks the the value with the given value
Here arr[ middle ]= =str (for CASE 3)
For CASE 1:
- The index of Highest Element changes to the index of Middle Element and then it again find out the middle value using same formula.
Here middle = (2-0) / 2 //here lowest index becomes "2"
= 1 - Now the compiler checks the the value with the given value
Here arr[ middle ]= =str (for CASE 1)
Example of Binary Search:-
@ Write a program to search an element in an array using Binary Search ?
@ Write a program to search an element in an array using Binary Search ?
- #include <stdio.h>
- int main()
- {
- int c, first, last, middle, n, search, array[100];
- printf("Enter number of elements\n");
- scanf("%d",&n);
- printf("Enter %d integers\n", n);
- for (c = 0; c < n; c++)
- scanf("%d",&array[c]);
- printf("Enter value to find\n");
- scanf("%d", &search);
- first = 0;
- last = n - 1;
- middle = (first+last)/2;
- while (first <= last) {
- if (array[middle] < search)
- first = middle + 1;
- else if (array[middle] == search) {
- printf("%d found at location %d.\n", search, middle+1);
- break;
- }
- else
- last = middle - 1;
- middle = (first + last)/2;
- }
- if (first > last)
- printf("Not found! %d isn't present in the list.\n", search);
- return 0;
- }
Linear Search V/S Binary Search
- In Linear Search the compiler will go to the very first element of an Array whereas in Binary Search the compiler will go to element present in the middle of an Array.
- In Linear Search the compiler will not need the array to be sorted. But in Binary Search the array should be Bubble Sort.
- Binary Search is faster and better then Linear Search Algorithm.
For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog. Thank you for your support and time...
0 Comments