DAA PRACTICAL
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
BUBBLE SORT:-
ALGORITHM:-
STEP 1. REPEAT STEP 2 & 3 FOR K=1 TO N-1
STEP 2. SET PTR:= 1
STEP 3. REPEAT WHILE PTR <= N-K
A) IF DATA [PTR]>DATA[PTR+1] THEN INTERCHANGE DATA[PTR] & DATA[PTR+1]
B) SET PTR:= PTR+1
STEP 4. EXIT
CODE:-
}
SELECTION SORT
ALGORITHM:-
STEP 1. REPEAT STEP 2 & 3 FOR K=1,2...N-1
STEP 2. MIN(A,K,N,LOC)
STEP 3. SET TEMP:=A[K],A[K]:=A[LOC] AND A[LOC];=TEMP
STEP 4. EXIT
CODE:-
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,arr[20],min=0,temp=0;
clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("\n");
for(i=0; i<n; i++)
{
printf("enter array element : ");
scanf("%d",&arr[i]);
}
printf("\nThe Unsorted array you entered : \n");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min])
{
min=j;
}
}
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
printf("\nThe Sorted array we got using Selection sort : \n");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
getch();
return 0;
}
MERGE SORT:-
ALGORITHM:-
STEP 1. SET L:=1
STEP 2. REPEAT STEP 3 TO 6 WHILE L<N;
STEP 3. CALL MERGEPASS (A,N,L,B)
STEP 4. CALL MERGEPASS(B,N,2*L,A)
STEP 5. SET L:=4*L
STEP 6. EXIT
CODE:-
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n1,n2,a1[20],a2[20],m[20];
clrscr();
printf("enter size of first array : ");
scanf("%d",&n1);
printf("\n");
for(i=0;i<n1;i++)
{
printf("enter array element : ");
scanf("%d",&a1[i]);
}
printf("\n");
printf("enter size of second array : ");
scanf("%d",&n2);
printf("\n");
for(i=0;i<n2;i++)
{
printf("enter array element : ");
scanf("%d",&a2[i]);
}
printf("\n\n the first array : ");
for(i=0;i<n1;i++)
{
printf("%d ",a1[i]);
}
printf("\n\n the second array : ");
for(i=0;i<n2;i++)
{
printf("%d ",a2[i]);
}
for(i=0;i<n1;i++)
{
m[i]=a1[i];
}
for(i=n1;i<n1+n2;i++)
{
m[i]=a2[i-n1];
}
printf("\n\n merged array : ");
for(j=0;j<n1+n2;j++)
{
printf("%d ",m[j]);
}
getch();
return 0;
}
BINARY SEARCH:-
ALGORITHM:-
STEP 1. SET BEG:=LB,END:=UB AND MID =INT(BEG+END)/2)
STEP 2. REPEAT STEP 3 & 4 WHILE BEG<=END AND DATA[MID] is not equal to ITEM
STEP 3. IF ITEM <DATA[MID],THEN
SET END:=MID-1
ELSE
SET BEG:=MID+1
STEP 4. SET MID:= INT(BEG+END)/2
STEP 5. IF DATA [MID] =ITEM, THEN
SET LOC;=MID
ELSE
SET LOC:=NULL
STEP 6. EXIT
CODE:-
MERGING AND SORTING:-
CODE:-
#include<stdio.h>
#include<conio.h>
int main()
{
int i, j, n1, n2, k, a1[20], a2[20], m[20];
clrscr();
printf("enter size of first array : ");
scanf("%d", &n1);
printf("\n");
for (i=0; i<n1; i++)
{
printf("enter array element :");
scanf( "%d", &a1[i] );
}
printf("\n");
printf("enter size of second array :");
scanf( "%d", &n2 );
printf("\n");
for(j=0; j<n2; j++)
{
printf("enter array element :");
scanf( "%d", &a2[j] );
}
printf("\n\n The first array :");
for(i=0; i<n1; i++)
{
printf("%d ", &a1[i]);
}
printf("\n\n The Second array :");
for(j=0; j<n2; j++)
{
printf("%d ", &a2[j]);
}
k=0;
i=0; j=0;
while(i<n1 && j<n2)
{
if(a1[i]<a2[j])
{
m[k++] = a1[i++];
}
else
{
m[k++] = a2[j++];
}
}
while(i<n1)
m[k++] = a1[i++];
while(j<n2)
m[k++] = a2[j++];
printf("\n\n Array after merging and sorting :");
for(i=0; i<n1+n2; i++)
{
printf("%d ",m[i]);
}
getch();
return 0;
}
Comments
Post a Comment
Kaushikmadhav77@gmail.com