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 :-
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
Comments
Post a Comment