HOME

Tuesday, April 18, 2017

LINEAR SEARCH- C PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,c=0, value, n, a[20];
    printf("Enter size of array\n");
    scanf("%d",&n);
    printf("Input % value\n",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter Value to Search\n");
    scanf("%d",&value);
    for(i=0;i<n;i++)
    {
        if(a[i]==value)
        {
            printf("\n%d found at position %d",a[i],i+1);
        c++;
        }
    }
    if(c==0)
    {
        printf("\nValue not found in list");
    }
getch();
}

SELECTION SORT - C PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j, temp, n, a[20];
    printf("Enter size of array\n");
    scanf("%d",&n);
    printf("Enter %d value\n",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    printf("sorted array is:\n");
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
    getch();
}

INSERTION SORT- C PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,n,a[20],temp;
    printf("Enter size of array\n");
    scanf("%d",&n);
    printf("\nInput %d value\n",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        j=i;
        while(j>0 && a[j-1]>a[j])
        {
            temp=a[j];
            a[j]=a[j-1];
            a[j-1]=temp;
            j--;
        }
    }
    printf("\nValue after sorted are:\n");
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);

    }
getch();
}

BUBBLE SORT - C PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],size,i,j,temp;
printf("Enter Size of Array\n");
scanf("%d",&size);
printf("Input %d value",size);
for(i=0;i<size;i++)
{
    scanf("%d",&a[i]);
}
for(i=size;i>1;i--)
{
    for(j=0;j<i-1;j++)
    {
        if(a[j]>a[j+1])
        {
            temp=a[j+1];
            a[j+1]=a[j];
            a[j]=temp;
        }
    }
}
printf("\nThe array element after sorting is\n");
for(i=0;i<size;i++)
{
    printf("%d\n",a[i]);
}
getch();
}