You are on page 1of 4

Assignment 3

1)Write a program to implement Bubble sort.

#include<stdio.h>
void main()
{
int elements[50],temp,n,i,a;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the %d integers :",n);
for(i=0;i<n;i++)
{
printf("\n Enter the number:");
scanf("%d",&elements[i]);
}
for(i=0;i<n;i++)
{
for(a=0;a<n-i;a++)
{
if (elements[a]>elements[a+1])
{
temp=elements[a];
elements[a]=elements[a+1];
elements[a+1]=temp;
}

}
}
printf("\n The Sorted list in ascending order is:");
for(i=0;i<n;i++)
printf("%d \n",elements[i]);
}

Output:
Enter the number of elements:10

Enter the 10 integers :


Enter the number:24
Enter the number:76

Enter the number:89

Enter the number:56

Enter the number:43

Enter the number:21

Enter the number:99

Enter the number:63

Enter the number:32

Enter the number:8

The Sorted list in ascending order is:


8
21
24
32
43
56
63
76
89
99

2) Write a Program to implement Linear search.

#include<stdio.h>
void main()
{
int elements[50],search,n,i;
printf("Enter the number of elements in array:");
scanf("%d",&n);
printf("\n Enter %d integer:\n ",n);

for(i=0;i<n;i++)
{
printf("\n The integer is:");
scanf("%d",&elements[i]);
}
printf("\n Now enter the number which is to be found:");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(elements[i]==search)
{
printf("\n The number %d you have searched is found at
%d.",search,i);
}}
}

Output:
Enter the number of elements in array:10

Enter 10 integer:

The integer is:3

The integer is:44

The integer is:52

The integer is:12

The integer is:88

The integer is:76

The integer is:95

The integer is:36


The integer is:67

The integer is:28

Now enter the number which is to be found:28

The number 28 you have searched is found at 9.

You might also like