Search
 
SCRIPT & CODE EXAMPLE
 

C

LINEAR AND BINARY SEARCH

#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int searchLinear(int a[], int n, int x)
{
    int i;
    for (i = 0; i < n; i++)
    {
        if (a[i] == x)
            return (i + 1);
    }
    return -1;
}

int searchBinaryNR(int a[], int l, int h, int x)
{
    int i, mid;
    while (l <= h)
    {
        mid = (l + h) / 2;
        if (a[mid] == x)
            return (mid + 1);
        else if (x > a[mid])
            l = mid + 1;
        else
            h = mid - 1;
    }
    return -1;
}

int searchBinaryR(int a[], int l, int h, int x)
{
    int i, mid;
    while (l <= h)
    {
        mid = (l + h) / 2;
        if (a[mid] == x)
            return (mid + 1);
        else if (x > a[mid])
            return searchBinaryR(a, mid + 1, h, x);
        else
            return searchBinaryR(a, l, mid + 1, x);
    }
    return -1;
}

void inputArray(int a[], int n)
{
    int i;
    printf("
 Enter array elements:");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
}
int main()
{
    int a[MAX], n, s, x, pos;
    while (1)
    {
        printf("
 Press 1 to search using Linear Search Algorithm");
        printf("
 Press 2 to search using Non - Recursive Binary Search Algorithm");
        printf("
 Press 3 to search using Recursive Binary Search Algorithm");
        printf("
 Press 4 to exit");
        printf("
");
        scanf("%d", &s);
        switch (s)
        {
        case 1:
            printf("
 Enter how many elements<= %d :", MAX);
            scanf("%d", &n);
            inputArray(a, n);
            printf("
 Enter the element to be searched : ");
            scanf("%d", &x);
            pos = searchLinear(a, n, x);
            break;
        case 2:
            printf("
 Enter how many elements<= %d :", MAX);
            scanf("%d", &n);
            inputArray(a, n);
            printf("
 Enter the element to be searched :");
            scanf("%d", &x);
            pos = searchBinaryNR(a, 0, n, x);
            break;
        case 3:
            printf("
 Enter how many elements<= %d :", MAX);
            scanf("%d", &n);
            inputArray(a, n);
            printf("
 Enter the element to be searched : ");
            scanf("%d", &x);
            pos = searchBinaryR(a, 0, n, x);
            break;
        default:
            exit(0);
        }
        if (pos != -1)
            printf("
 Element found at %d position", pos);
        else
            printf("
 Element not found");
    }
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: what is %d in C 
C :: *= operator 
C :: what is clrsce in C 
C :: ecto where is not nil 
C :: arduino dont working dc motor 
C :: address operator 
C :: git add -u flag 
C :: fifo page algorithm in C 
C :: c program for calculating product of array 
C :: C static libraries (Indexing an archive) 
C :: How can you invoke the constructor from the parent class? *ruby 
C :: ansi c write unsigned short to file 
C :: divide a linked list into three parts based on their position mod 3. 
C :: uninstall elgg from hostgtor 
C :: Computers round off numbers 
C :: how to output in green in c 
C :: online c compiler for graphics 
C :: convert char to int ascii in c function 
C :: reverse binary tree c 
C :: garbage collection and dangling reference 
C :: write the data in perticulare memmory loaction in C 
C :: program to merge two strings in c 
C :: creating an array of arrays or 2D array dynamically 
C :: To get file info 
C :: wordpress clean name spaces 
C :: array of string in c 
C :: C Variable Byte Sizes 
Dart :: flutter sleep 
Dart :: const text style flutter 
Dart :: canonical tag 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =