DAA Practicals


LINEAR SEARCH:-


ALGORITHM:-    

 STEP 1.  Begin

STEP 2. set a[5] {10,20,30,40,50}

STEP3. set i=0

STEP4. input searching item

STEP5. repeat step 6 & 7 while i<5

STEP6. if a [i]=item then print item found and location=i & exit

STEP 7. i=i+1

STEP8. if i>5  then print item not found

STEP 9. EXIT



CODE :-      


  #include<stdio.h>

#include<conio.h>

void main() {

    int a[5], i = 0, j, item;

    clrscr();

    printf("Enter array elements:\n");

    for (j = 0; j < 5; j++) {

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

    }

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

    scanf("%d", &item);

     while (i < 5) {

        if (a[i] == item) {

            printf("Item found at location: %d\n", i);

            break; // Added to exit the loop once the item is found

        }

        i++;

    }

  if (i == 5) {

        printf("Item not found\n");

    }

getch();

}



INSERTION IN ARRAY:-


ALGORITHM:-    

 STEP 1. set j=n

 STEP2. repeat step 3 and 4 while j>=k

 STEP3. set LA[j+1];=LA[J]

 STEP4. SET J:= J-1

 STEP5. SET LA[K]:=ITEM

 STEP6.  SET N:=N+1

 STEP7. EXIT


CODE :- 


#include<stdio.h>
#include<conio.h>

void main() {
    int a[5], i = 0, j, position, value, size = 5;
    clrscr();
     printf("Enter array elements:\n");
   
    for (j = 0; j < 5; j++) {
        scanf("%d", &a[j]);
    }
   
    printf("Enter the position to insert (0-based index): ");
    scanf("%d", &position);
   
    if (position < 0 || position > size) {
        printf("Invalid position. Insertion failed.\n");
    } else {
        printf("Enter the value to insert: ");
        scanf("%d", &value);
        for (i = size - 1; i >= position; i--) {
            a[i + 1] = a[i];
        }
        a[position] = value;
        size++;
        printf("Array after insertion:\n");
        for (i = 0; i < size; i++) {
            printf("%d ", a[i]);
        }
        printf("\n");
    }

    getch();
}


TRAVERSING ARRAY:-


ALGORITHM:-

STEP1. SET K=LB

STEP2. REPEAT STEP 3 & 4 WHILE K<=UB

STEP3. VISIT ELEMENT LA[K]

             SET K :=K|+1

STEP4. EXIT


CODE:-.


#include<stdio.h>

#include<conio.h>

void main() {

    int a[5], i, size = 5;

    clrscr();

    printf("Enter array elements:\n");

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

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

    }

    printf("Array elements during traversal:\n");

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

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

    }

    printf("\n");

    getch();

}


INSERTION SORT :-


ALGORITHM:=

STEP1. SET A[0]:= INFINITY

STEP2. REPEAT STEP 3 TO 5 FOR K =2,3,...N

STEP3. SET TEMP:= A[K] AND PTR:=K-1

STEP 4. REPEAT WHILE TEMP<A[PTR]

               A) SET A[PTR+1]:=A[PTR]

               B) set PTR:=PTR-1

STEP5. SET A[PTR+1];=TEMP

STEP6. RETURN


CODE:-

#include<stdio.h>
#include<conio.h>
void insertionSort(int arr[], int size) {
    int i, j, key;
    for (i = 1; i < size; i++)
    {
        key = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }

        arr[j + 1] = key;
    }
}

void main() {
    int a[5], i, size = 5;
    clrscr();
    printf("Enter array elements:\n");
    for (i = 0; i < size; i++) {
        scanf("%d", &a[i]);
    }
    printf("Array before sorting:\n");
    for (i = 0; i < size; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
    insertionSort(a, size);
    printf("Array after insertion sort:\n");
    for (i = 0; i < size; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
    getch();
}



DELETION IN ARRAY:-


ALGORITHM:-



 

Comments

Popular posts from this blog

BCA- Computer Fundamental Old Question Paper

BCA 1ST SEMESTER NOTES

JAVA