Algorithms - CodingTute https://codingtute.com/tag/algorithms/ Learn to code in an easier way Wed, 22 Jun 2022 13:45:48 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 https://codingtute.com/wp-content/uploads/2021/06/cropped-codingtute_favicon-32x32.png Algorithms - CodingTute https://codingtute.com/tag/algorithms/ 32 32 187525380 Binary Search https://codingtute.com/binary-search/ Wed, 22 Jun 2022 13:45:44 +0000 https://codingtute.com/?p=3540 Binary search is a searching algorithm, it applies to finding an element in a sorted array. In the binary search, we will divide the half by comparing the element to be searched with the middle element of the array. If the array is not sorted you must use sorting techniques like merge sort or quick ... Read more

The post Binary Search appeared first on CodingTute.

]]>
Binary search is a searching algorithm, it applies to finding an element in a sorted array. In the binary search, we will divide the half by comparing the element to be searched with the middle element of the array. If the array is not sorted you must use sorting techniques like merge sort or quick sort, etc, and perform a binary search operation. As we deal with the sorted arrays it is a best case that performs a minimum number of steps.

The time complexity of the binary search algorithm is O(log n).

How does Binary Search Work?

Binary search applies only to the sorted arrays. The search element starts by comparing with the middle element in the array.

  • If the value matches then it returns its position.
  • In case, the search element is less than a middle element of the array (considering the array is in ascending order) discard the second half of the array and the search continues by dividing the first half of the array.
  • Similarly, if the search element is greater than a middle element of the array(considering the array is in ascending order)discard the first half of the array and the search continues by dividing the second half of the array.

Binary Search can be performed in two ways:

  1. Recursion Method
  2. Iterative Method

Binary Search Recursive Method

Binary Search in C using Recursion

#include <stdio.h>
int binarySearch(int arr[], int l, int r, int search) 
{ 
    if (r >= l) { 
        int mid = l + (r - l) / 2; 
        if (arr[mid] == search) 
            return mid; 
        if (arr[mid] > search) 
            return binarySearch(arr, l, mid - 1, search); 
        return binarySearch(arr, mid + 1, r, search); 
    } 
    return -1; 
} 
int main() 
{ 
    int arr[] = { 7, 9, 4, 11, 4 }; 
    int n = sizeof(arr) / sizeof(arr[0]); 
    int search = 11; 
    int position = binarySearch(arr, 0, n - 1, search); 
    (position == -1) ? printf("The element is not present in array") 
                   : printf("The element is present at index %d", 
                            position); 
    return 0; 
}

Output

The element is present at index 3

Explanation:

In the above Binary search, we use the recursion method,binarySearch() function repeatedly calls itself until the base condition reached some specified condition which is discussed below:

  • If the arr[mid] is greater than the search element, we call the binarySearch(arr, l, mid – 1, search) with the start as mid+1.
  • If the arr[mid] is less than the search element, we call the binarySearch(arr, l, mid + 1, search) with the end as mid-1.
  • There are two exit conditions for this recursion:
    • If the element is found then return mid.
    • If the search element is not found, it will return -1, i.e start is greater than the end.

Binary Search Iterative Method

Binary Search in C using Iterative

#include <stdio.h>
int binarySearch(int arr[], int n, int search)
{
    int low = 0, high = n - 1;
 
    while (low <= high)
    {
        int mid = (low + high)/2;    
        if (search == arr[mid]) {
            return mid;
        }
        else if (search < arr[mid]) {
            high = mid - 1;
        }
        else {
            low = mid + 1;
        }
    }
    return -1;
}
 
int main()
{
    int arr[] = {  7, 9, 4, 11, 4};
    int search= 11;
 
    int n = sizeof(arr)/sizeof(arr[0]);
    int index = binarySearch(arr, n, search);
 
    if (index != -1) {
        printf("Element found at index %d", index);
    }
    else {
        printf("Element not found in the array");
    }
 
    return 0;
}

Output

Element found at index 3

Explanation:

In the above Linear Search, we use the iterative method, which is looping over the same block of code multiple times until some specified conditions discussed below-

  • If the search element is equal to arr[mid], then it returns mid.
  • If the search element is less than arr[mid], then high = mid – 1 until the element is found.
  • If the search element is greater than arr[mid], then low = mid + 1 until the element is found.

The exit condition for this iterative is:

  • if the element is not found, then it returns -1,i.e it is low is greater than high.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post Binary Search appeared first on CodingTute.

]]>
3540
Linear Search https://codingtute.com/linear-search/ Tue, 21 Jun 2022 07:45:08 +0000 https://codingtute.com/?p=3507 Linear Search is a simple sequential search algorithm. In Linear Search, we will traverse the array or list sequentially until the element is found. The main disadvantage of linear search is time complexity. As we deal with unorder data an element can be present at the end of the list, so the worst case could ... Read more

The post Linear Search appeared first on CodingTute.

]]>
Linear Search is a simple sequential search algorithm. In Linear Search, we will traverse the array or list sequentially until the element is found. The main disadvantage of linear search is time complexity. As we deal with unorder data an element can be present at the end of the list, so the worst case could be traversing the array or list till the end.

The time complexity of a linear search is O(n).

Linear Search in C

#include <stdio.h>
long linear_search(long [], long, long);
int main()
{
   int array[5]={4,66,72,5,8}, search, i, n, location;
   printf("Enter search element\n");
   scanf("%d", &search);
   location = linear_search(a, n, search);
   if (location == -1)
      printf("%d isn't present in the array.\n", search);
   else
      printf("%d is present at location %d.\n", search, position+1);
   return 0;
}
int linear_search(int a[], int n, int find) 
{
   int i;
   for (i = 0 ;i < n ; i++ ) {
      if (a[i] == find)
         return i;
   }
   return -1;
}

Output

Enter search element
72
72 is present at location 3.

Explanation:

In the above Linear search program, we traverse the array using for loop and it compares each element with the element to be found. If the element is found it will return the index else it will return -1.

At each iteration, we will compare each element with the search element.

  • If Element matches, then it will return its corresponding index.
  • If not it will move to the next index.
  • If an element does not match with any element from the given array then it will return -1.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

The post Linear Search appeared first on CodingTute.

]]>
3507