0% found this document useful (0 votes)
92 views12 pages

Practical File MDC

Practical File MDC

Uploaded by

harrysam0066
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views12 pages

Practical File MDC

Practical File MDC

Uploaded by

harrysam0066
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

COMPUTER

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)

printf(The number is found');


exit(0);

printf("The number is not found");


getch();

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

position in the list. largest number, 21,


has
At the end of 13, 23, 27, 33, 51, moved
Pass 7(last 57, 66, 85
to its position inthe lis pass), the seventh largest nunber.
13, 23, 27, 33, 51, 3,has r«ved
Algorithm: 57, 66, 85
Stepl :/Input the number of items)
Read n
Step2:[Read Nelementsfrom keyboard]
For I -0 to n -1
Read a[i)
End for loop
Step3: [Sort the elements] //Step3: About Bubble sort algorithm
For i= lto n -1 do
For j= 0 to n -i-1 do
If( a[j]>=a[j+1])
Temp = ajl
alj]= alj+1]
aj+1]=Temp
End if

End for loop


End for loop
Step4: [Print sorted elements]
For i=0 to n -1
Print a[i]
End for loop
Step5 :[Stop]
3 Program:For the implementation of Bubble Sort.
#include<stdio.h>
#includexconio.h>
void main()
COMPUTER 391
FUNDAMENTALS &PROGRAMMINGINC

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;

printf("After sorting: ");


for(i=O;i<n;i++)
printf("%d",ali]);
getch():

OutEnterputtotal: Inumbers of elements: 6


Enter 6 elements:45
23
0

89
Afer sorting: 0 11
23 45
COMPUTER FUNDAMENTALS &PROGRAMMING INC

Step4: [Print sorted elements]


For i=0 to n-1
Print a<i]
End for loop
Step5:(Stop]
4 Program :For the implementation of SelectionSort.
#include<stdio.h>
#includeKconio.h>
void main(){
int n,ij,temp,pos, min,a[10];
clrscr();
printf("Enter size of array:");
scanf(%d",&n);
printf(nEnter %d elements: n):
for(i=0;i<n;it+)
scanf("%d",&a[il);
for(i=0;i<=n-1;i++)
min = a[i);
for(j=i+1;j<n;j++)
if(a[i]< min)

min = a[jl;
pos =j;

temp=a[pos]:
a[pos]=a[i);
ali<=temp;
COMPUTERR
FUNDAMENTALS &PROGRAMMING IN C 395

intf(nAfter sorting elements are:):


for(i-0;i<n;i++)
printf( %d",a[i]);
getch);

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;

printf("After sorting: "):


for(i-0;i<n;i++)
printf( %dt".a[i]):
getch0);

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("The merged array: "):


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

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

You might also like