Linear and Binary Search

 


Linear and Binary Search

Linear Search

Introduction:

Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.


Algorithm:

We assume list is an array of n elements. We then implement the search function.

Linear Search ( Array A, Value x) 

 

Step 1: Set i to 1

Step 2: if i> n then go to step 7

Step 3: if A[i] = x then go to step 6

Step 4: Set i to i + 1

Step 5: Go to Step 2

Step 6: Print Element x Found at index i and go to step 8

Step 7: Print element not found

Step 8: Exit

 

Code:

 

// C++ code to linearly search

// x in arr[]. If x is present

// then return its location,

// otherwise return -1

#include <iostream>

using namespace std;

 

int search(int arr[],

                        int n, int x)

{

            int i;

            for (i = 0; i< n; i++)

                        if (arr[i] == x)

                                    return i;

            return -1;

}

 

// Driver code

int main(void)

{

            int arr[] = {2, 3, 4, 10, 40};

            int x = 10;

            int n = sizeof(arr) / sizeof(arr[0]);

 

            // Function call

            int result = search(arr, n, x);

            (result == -1) ?

            cout<< "Element is not present in array" :

            cout<< "Element is present at index " <<

                                    result;

            return 0;

}

 

    Output:

 

 

 

Binary Search

Introduction:

Binary search is a fast search algorithm. This searchalgorithm works on the principle of divide and conquer. For this algorithm to work properly,the data collection should be in the sorted form.

 

Binary Search

For a binary search to work, it is mandatory for the target array to be sorted. We shalllearn the process of binary search with a pictorial example. The following is our sortedarray and let us assume that we need to search the location of value 31 using binary search.

First, we shall determine half of the array by using this formula.

 

mid = low + (high - low) / 2

 Here it is, 0 + (9 - 0) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.

Now we compare the value stored at location 4, with the value being searched, i.e. 31.

 We find that the value at location 4 is 27, which is not a match. As the value is greaterthan 27 and we have a sorted array, so we also know that the target value must be in the upper portion of the array.

Code:

// C++ program to implement iterative Binary Search

#include <bits/stdc++.h>

using namespace std;

 

// An iterative binary search function.

int binarySearch(int arr[], int l, int r, int x)

{

            while (l <= r) {

                        int m = l + (r - l) / 2;

 

                        // Check if x is present at mid

                        if (arr[m] == x)

                                    return m;

 

                        // If x greater, ignore left half

                        if (arr[m] < x)

                                    l = m + 1;

 

                        // If x is smaller, ignore right half

                        else

                                    r = m - 1;

            }

 

            // If we reach here, then element was not present

            return -1;

}

 

// Driver code

int main(void)

{

            int arr[] = { 2, 3, 4, 10, 40 };

            int x = 10;

            int n = sizeof(arr) / sizeof(arr[0]);

            int result = binarySearch(arr, 0, n - 1, x);

            (result == -1)

                        ? cout<< "Element is not present in array"

                        : cout<< "Element is present at index " << result;

            return 0;

}

Share this:

ABOUT THE AUTHOR

Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible

0 comments:

Post a Comment