Binary Search

#include<stdio.h>

int binarySearch(int arr[], int left, int right, int value){
    while (left<=right)
    {
        int mid = left + (( right-left)/2);
        if(arr[mid]==value){
            return mid;
        }
        if(arr[mid]<value){
            left=mid+1;
        }
        else{
            right=mid-1;
        }

    }
    return -1;
}

int main(){
    int arr[5]= {2,3,4,10,40};
    int value=40;
    int n=5;
    int result = binarySearch(arr,0,n-1,value);
    if(result==-1){
        printf("value is not present");
    }
    else{
        printf("index of the value is: %d\n",result);
    }
}
Show More

Linear Search

#include <stdio.h>

int main() {

int arr[5]={2,3,4,10,40};
int value;
printf("Take searching value");
scanf("%d", &value);
int arr_size=5;
int i=0;

for (i=0;i<arr_size;i++)
{
    if(arr[i]==value){
    printf("value %d found at index %d",value,i);}
    }

    return 0;
}
Show More

Stack

#include <stdio.h>

int stack[100], n=100, top=-1;
void push(int x)
{
    if(top>=n-1)
    {printf("stack overflow");}
        
    else
    {
        top=top+1;
        stack[top]=x;
    }
    
}
void pop()
{
    
    if(top==-1)
    {printf("stack underflow");}
        
    else
    {
        top=top-1;
    }
    
}

void IsEmpty()
{
    if(top==-1)
    {printf("Empty");}
    else
    {printf("Not Empty");}
    
}
int main() {

push(10);
push(2);
pop();
IsEmpty();

    return 0;
}
Show More

String Match

#include <stdio.h>

int main() {
char str1[14]={'a','a','b','c','c','a','t','d','b','a','f','a','b','d'};
char str2[3]={'c','a','t'}; // 11 

int str1_size=14;
int str2_size=3;

int i=0,j=0,m=0;

for(i=0;i<str1_size-str2_size;i++)
{
    
    for(j=0;j<str2_size;j++)
    {
        if(str1[i+j]==str2[j])
        {
            m=m+1;
        }
        else
        {
            m=0;
            break;
        }
    }
    if(m==str2_size)
    {printf("Matched\n");}
}

    return 0;
}
Show More