Tuesday, November 16, 2021

Wednesday, October 20, 2021

Program 3- Linear Search in C Language

Program 3: Linear search
***********************************************

#include <stdio.h>

int main()

{

    int a[20],i,x,n, c;

    printf("How many elements you are going to enter?");

    scanf("%d",&n);

    printf("Enter array elements:");

    for(i=0;i<n;++i)

        scanf("%d",&a[i]);

         printf("\n Enter element to search:");

    scanf("%d",&x);

       for(i=0;i<n;++i)

        if(a[i]==x)

        { printf("Element %d found at index %d \n",x, i);

            c=c+1;

                    }

         if(c>0)

        printf("Element %d found %d times ",x, c);

    else

        printf("Element not found");

    return 0;

}

Outputs



Program 2- Selection Sort in C Language

Program 1: Selection Sort
****************************************************

#include <stdio.h>

int main()

{   int i, j, n, temp, arr[25];

   printf("How many numbers u are going to enter?: ");

   scanf("%d",&n);

   printf("Enter %d elements: ", n);

   // Loop to get the elements stored in array

   for(i=0;i<n;i++)

      scanf("%d",&arr[i]);

 

   // Logic of selection sort algorithm

   for(i=0;i<n;i++){

      for(j=i+1;j<n;j++){

         if(arr[i]>arr[j]){

            temp=arr[i];

            arr[i]=arr[j];

            arr[j]=temp;

         }

      }

   }

   printf("Sorted elements: ");

   for(i=0;i<n;i++)

      printf(" %d",arr[i]);

    return 0;

}

Output

Program 1- Insertion Sort in C language

Program 1: Insertion Sort
***************************************************

#include <stdio.h>
int main()
{   int i, j, n, temp, arr[25];
   printf("How many numbers u are going to enter?: ");
   scanf("%d",&n);
   printf("Enter %d elements: ", n);
   // This loop would store the input numbers in array
   for(i=0;i<n;i++)
      scanf("%d",&arr[i]);
   // Implementation of insertion sort algorithm
   for(i=1;i<n;i++){
      temp=arr[i];
      j=i-1;
      while((temp<arr[j])&&(j>=0)){
         arr[j+1]=arr[j];
         j=j-1;
      }
    arr[j+1]=temp;
   }
   printf("Order of Sorted elements: ");
   for(i=0;i<n;i++)
      printf(" %d",arr[i]);
    return 0;}

Output

Thursday, February 18, 2021

Saturday, January 30, 2021

Saturday, January 16, 2021

Books