Practical File MDC
Practical File MDC
FUNDAMENTALS. &PROGRAMMINGINC
381
Cprogramcode for linear search is :
The
inchude<stdio.h
sinclude<conio.h>
voidmain()
inta(10].i,n,key;
printf("Enterthe number offitems:):
scanf("%d",&n):
n):
printf("Enter the %d numbers:",
for(i-0;i<=n-1;i++)
scanf(""%d",&a[il);
search:);
printf(Enter the key item which you want to
scanf(%d",&key);
for(i-=0;i<=n-1;i++)
if(a[i]==key)
Output:
Enter the number of items: 5
Enter the 5 numbers: 24 34 58 77 13 search: 77
Enter thekey iten which you wantto
The number is found
Key Item
Program for Multiple Existence of
#include<stdio.h>
#include<conio.h>
void main()
int a[100], key,, i, n, count =0;
clrscr);
dlld noL SOrEL
it is better to sort them and use binary search.
The complete Cprogram is shown below:
#include<stdio.h>
Binay eanch]
#include<conio.h>
void main()
int a[ 10],i,n,item,low,high,mid;
printf(Enter the size of an array:);
scanf("9%d",&n);
printf("Enter the elements in ascending order: );
for(i-0;i<n;i++)
COMPUTER FUNDAMENTALS& PROGRAMMING INC 387
scant("%d"&ali):
printf("Enter the item to be search: ):
scanf("%d",&item);
low=0;
high=n-1:
while(low<=high)
mid=(low+high)/2;
if(item==a[mid])
printf("Successful Search.n"):
exit(0);
else if(item<a<mid])
high=mid-1;
else
low=mid+1;
printf("Un-successful Search.n");
Outputl:
Enter the size of an array: 5
4 78 11 21
Enter the elements in ascending order:
Enter the item to be search: 11
Successful Search.
Output2:
Enter the size of an array:
Enter the elements in ascending order: 47811 21
Enter the item to be search: 66
Un-successful Search.
Advantages :
1. Simple technique.
2.
Very efficientt searching technique.
&
At the end of PROGRAMMING IN
Pass 6, the sixth
C
int n, temp,ij,a[20];
printf("Enter total
numbers of elements: ");
scanf("%d",&n);
printf(""Enter %d
for(i-0;i<n;i++) lelements:",n);
scanf("%d",&a<il);
I/Bubble sorting algorithm
for(i=1;i<=n-1;i++)
for(j=0;j<-n-i-l;j++)
if(alj]>=alj+1)
temp=a[j]:
aljl=alj+1):
alj+1]=temp;
89
Afer sorting: 0 11
23 45
COMPUTER FUNDAMENTALS &PROGRAMMING INC
min = a[jl;
pos =j;
temp=a[pos]:
a[pos]=a[i);
ali<=temp;
COMPUTERR
FUNDAMENTALS &PROGRAMMING IN C 395
Output:
Enter size of array: 6
Enter 6elements: 56 112 58 22 43
After sorting elements are: 2 11 22 43 56 58
Complexity of the Selection Sort Algorithm
algorithm is independent
Print a[i]
End for loop
Step 5:[Stop]
5 Program: For the implementation of Insertion Sort.
#include<stdio.h>
#includeKconio.h>
void main()
int ij.n,temp,a[201:
printf("Enter total elements: ):
cOMPUTER
scanf("%d",&n);
FUNDAMENTALS PROGRAMMING &
IN C 397
printf("Enter %d
for(i-0;i<n;i++)
ielements: ",n);
scanf("%d",&a[i]):
for(i=1;i<n;i++)
temp=a[i]:
j=i-l;
while(temp<a[j])&&(j>=0)
alj+1]=a[jl:
j=j-1;
a[j+1]=temp;
Output:
Enter total elements: 5
Enter 5elements: 87
67
2
Aiter sorting:0 2 33 67 87
Cominpsleerxtiitoyn
The
of Insertion Sort:
sort algorithmis a very slow algorithm when nis very
large.
[End of Step 6 While Loop]
Ored Step8: [Stop]
Write aC' program for the implementation of above merging algorithm.
IMerging of two Sorted Arrays
#include<stdio.h>
he #include<conio.h>
void main()
{
int a[10], b[1 0],c[20], i,j,k=0, m,n;
clrscr();
printf("Enter the size of 1st Aray:n");
scanf("%d",&m);
printf("Enter the elements of lst Array in ascending order:\n"):
for(i-0;i<m;i++)
scanf("%d'",&a[i);
printf("Enter the size of 2nd Array:n'");
scanf("%d', &n);
printf("Enter rrayin ascending order: n");
the elements of 2nd Array
cOMPUTER FUNDAMENTALS &PROGRAMMING INC
for(j=0;j<nj++)
scanf("%d",&blil):
)
i-0,j-0;
while(i<m && j<n)
if(a<il<bjl)
c[k]=a[i];
k++;
else if(ali}>b[j])
c[k<=b[jl:
jt;
k++;
else if(a[il==b[j)
c[k<=a[i);
itt;
k++;
c[k]=bljl:
j++;
k++;
while(i<m)
401
AMPUTERFUNDAMENTALS
CO
&
PROGRAMMINGIN C
c[kl=a[i):
k+t;
itt;
while({j<n)
c(k<=bljl:
k+t;
printf("%d°",cli):
getch0;
Output :
Enter the size of 1st Array:
4
ascending order:
Enter the elements of 1st Array in
23
S6
78
98
Enter the size of 2nd Array:
3
ascending order:
Enter the elements of 2nd Array in
12
43
56 78 98
The 23 43
merged array: 0 12