Showing posts with label Searching. Show all posts
Showing posts with label Searching. Show all posts

Tuesday, 26 February 2019

Iterative Binary Search in C using function

Binary search algorithm is one of the most efficient searching algorithm. The precondition of binary search algorithm is that the set of elements e.g array must be ordered (sorted) in particular order.
Binary search begins by finding the middle element of the set and comparing it with the key value. If key value is smaller than middle element, it means all elements from middle element to last element of the set are greater than key value, so they must not be searched and should be discarded. So now the new set should comprise of elements from first to element just before middle element. If key value is greater than middle element, it means all elements from first element to middle element of the set are smaller than key value, so they must not be searched and should be discarded. So now new set should comprise of elements from first element after middle element to last element. If key value is equal to middle element, then index of middle element is returned.
//Iterative binary search in C using function
#include <stdio.h>
int main()
{
    int array[] = {9,10,12,15,32,50};
    int key = 17,arraySize=0,i,result=-1;
    //function declaration
    int binarySearch(int array[],int n,int value);
    //finding the size(no of elements) of array
    arraySize = sizeof(array)/ sizeof(int);
    //function calling
       result=binarySearch(array,arraySize,key);
      
       if(result!=-1)
         printf("Element %d found at position %d.\n",key,result+1);
       else
         printf("Element %d does not exist.\n",key);

    return 0;
}

//function definition
      int binarySearch(int array[],int n,int value){
            int index=-1,i,first,last,mid;
           
    first=0;
    last=n-1;
       while(first<=last)
    {
        mid=(first+last)/2;//index of middle element
        if(value<array[mid])
        {
            last=mid-1;
        }
        else if(value>array[mid])
            first=mid+1;
        else{
            index=mid;
            break;
        }
Output: key=10;
Output: key=32;

Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

Monday, 25 February 2019

Iterative Binary Search in C without using function

Binary search algorithm is one of the most efficient searching algorithm. The precondition of binary search algorithm is that the set of elements e.g array must be ordered (sorted) in particular order.

Binary search begins by finding the middle element of the set and comparing it with the key value. If key value is smaller than middle element, it means all elements from middle element to last element of the set are greater than key value, so they must not be searched and should be discarded. So now the new set should comprise of elements from first to element just before middle element. If key value is greater than middle element, it means all elements from first element to middle element of the set are smaller than key value, so they must not be searched and should be discarded. So now new set should comprise of elements from first element after middle element to last element. If key value is equal to middle element, then index of middle element is returned.

//Iterative binary search in C without using function
#include <stdio.h>
int main()
{
    int array[] = {9,10,12,15,32,50};
    int key = 17,arraySize=0,i,index=-1,first,last,mid;

    //finding the size(no of elements) of array
    arraySize = sizeof(array)/ sizeof(int);

    first=0;
    last=arraySize-1;
    while(first<=last)
    {
        mid=(first+last)/2; //index of middle element
        if(key<array[mid])
        {
            last=mid-1;
        }
        else if(key>array[mid])
            first=mid+1;
        else{
            index=mid;
            break;
        }
           
    }

    if(index!=-1)
        printf("Element %d found at position %d.\n",key,index+1);
    else
        printf("Element %d not exist in given set of elements.\n",key);

    return 0;
}
Output: key=15;
Output: key=10;
Output: key=17;

Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

Thursday, 21 February 2019

Linear Search using function in C

Linear Search

  1. Traverse given array starting from first element.
  2. Compare element at current index with given key.
  3. If key matches with an element, return the index.
  4. If key doesn’t match with any of elements, return -1.
//C program for linear search using function
#include <stdio.h>
     int main(void)
      {
       int array[] = { 10, 15, 9, 12, 50,32 };
       int key = 90,arraySize=0,i,result;
       //function declaration
       int linearSearch(int array[],int n,int value);
      //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
       //function calling
       result=linearSearch(array,arraySize,key);
       if(result!=-1)
         printf("Element %d found at position %d.\n",key,result+1);
       else
         printf("Element %d does not exist.\n",key);
               return 0;
      }   
      //function definition
      int linearSearch(int array[],int n,int value){
      int index=-1,i;
       for (i = 0; i < n; i++){                 
         if (array[i] == value){
            index=i;  //key found at index i
            break;
         }          
       }
               return index;

      }



Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

Linear Search in C without using function

In Searching, you are given a set of elements and a key, you have to find the location of given key in that set.
These algorithms are generally classified into two categories:
  1. Sequential Search: In this, the set of elements is traversed sequentially and every element is checked against key. For example: Linear Search.
  2. Interval Search: These algorithms are used when elements in the given set are sorted. These algorithms are more efficient than Linear Search.For Example: Binary Search.
(1).Linear Search

  1. Traverse given array starting from first element.
  2. Compare element at current index with given key.
  3. If key matches with an element, return the index.
  4. If key doesn’t match with any of elements, return -1.
 
  Problem: Given an array array[] of size n, write a program in C to search a given element x in array[].
   Examples :
     Input : array[] = { 10, 15, 9, 12, 50,32 };
     key = 15;
     Output : 2
     Element key is present at index 2.
   
     Input : array[] = { 10, 15, 9, 12, 50,32 };
     key = 65;
     Output : -1
     Element key does not exist in given set of elements.
  
Solution:
#include <stdio.h>
     int main()
      {
       int array[] = { 10, 15, 9, 12, 50,32 };
       int key = 9,arraySize=0,i,index=-1;
      
     //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(int);
      
       for (i = 0; i < arraySize; i++){             
         if (array[i] == key){
            index=i;  //key found at index i
         }          
       }
      
       if(index!=-1)
         printf("Element %d found at position %d.\n",key,index+1);
       else
         printf("Element %d not exist in given set of elements.\n",key);
              
       return 0;
      }    

Please comment if you find anything incorrect, or you want to improve the topic discussed above.