0% found this document useful (0 votes)
76 views6 pages

Ex. No: 6.a Linear Search Aim

The document describes a C program to implement bubble sort. The algorithm involves repeating steps to compare adjacent elements in an array and swap them if they are in the wrong order until the array is fully sorted. The program takes user input of numbers, performs bubble sort on the array, and prints the sorted numbers. It successfully implements the concept of bubble sort in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views6 pages

Ex. No: 6.a Linear Search Aim

The document describes a C program to implement bubble sort. The algorithm involves repeating steps to compare adjacent elements in an array and swap them if they are in the wrong order until the array is fully sorted. The program takes user input of numbers, performs bubble sort on the array, and prints the sorted numbers. It successfully implements the concept of bubble sort in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex. No: 6.

a LINEAR SEARCH

AIM:
To write a C program to implement the concept of linear search.

ALGORITHM:

Step 1: Start with the first item in the list.

Step 2: Compare the current item to the target

Step 3: If the current value matches the target then we declare victory and stop.

Step 4: If the current value is less than the target then set the current item to be the next item
and repeat from 2.

Step 5: Stop.

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main() {

int arr[20];

int i,size,sech;

printf("\n\t-- Linear Search --\n\n"); printf("Enter total no. of elements : ");


scanf("%d",&size);
for(i=0; i<size; i++) {

printf("Enter %d element : ",i+1);

scanf("%d",&arr[i]);

printf("Enter the element to be searched: ");


scanf("%d",&sech);

for(i=0; i<size; i++) {

if(sech==arr[i]) {

printf("Element exits in the list at position : %d",i+1);

break;

getch();

}
+ TEAM
OUTPUT:

-- Linear Search --

Enter total no. of elements : 5

Enter 1 element : 10

Enter 2 element : 4

Enter 3 element : 2

Enter 4 element : 17

Enter 5 element : 100

Enter the element to be searched: 17

Element exits in the list at position : 4

RESULT:

Thus a C program for the concept of linear search was implemented successfully.
V+ TEAM
Ex. No: 6.b BINARY SEARCH

AIM:
To write a C program to implement the concept of binary search.

ALGORITHM:

Step 1: Set the list to be the whole list

Step 2: Find the middle value of the list

Step 3: If the middle value is equal to the target then we declare victory and stop.

Step 4: If the middle item is less than the target, then we set the new list to be the upper half of
the old list and we repeat from step 2 using the new list.

Step 5: If the middle value is greater than the target, then we set the new list to be the bottom
half of the list, and we repeat from step 2 with the new list.

Step 6: Stop.

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main(){

int n,i,search,f=0,low,high,mid,a[20];

clrscr();

printf("Enter the n value:"); scanf("%d",&n); for(i=1;i<=n;i++){


printf("Enter the number in ascending order a[%d]=",i);

scanf("%d",&a[i]);

printf("Enter the search element:");


scanf("%d",&search);

low=1; high=n; while(low<=high){ mid=(low+high)/2; if(search<a[mid]){ high=mid-1;


}

else if(search>a[mid]){

low=mid+1;

} else{ f=1;
printf("obtained in the position %d:",mid);

getch();

exit();

}}

if(f==0)

printf("not present");

getch();

}
+ TEAM
OUTPUT:
Enter the n value: 5

Enter the number in ascending order a[1]=10

Enter the number in ascending order a[2]=8

Enter the number in ascending order a[3]=9

Enter the number in ascending order a[4]=24

Enter the number in ascending order a[5]=1

Enter the search element:9 obtained in the position 3:

RESULT:

Thus a C program for the concept of binary search was implemented successfully.
Ex. No: 7.a BUBBLE SORT

AIM:
To write a C program to implement the concept of bubble sort.

ALGORITHM:
Step 1: Start.
Step 2: Repeat Steps 3 and 4 for i=1 to 10
Step 3: Set j=1
Step 4: Repeat while j<=n
(A) if a[i] < a[j]
Then interchange a[i] and a[j] [End of if]
(B) Set j = j+1
[End of Inner Loop]
[End of Step 1 Outer Loop]
Step 5: Stop.

PROGRAM:
#include<stdio.h>

#include<conio.h>

void main(){

int n, i, j, temp , a[100];

printf("Enter the total integers you want to enter (make it less than 100):\n");

scanf("%d",&n);

printf("Enter the %d integer array elements:\n",n);

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

scanf("%d",&a[i]);

for(i=0;i<n-1;i++){ for(j=0;j<n-i-1;j++){ if(a[j+1]<a[j]){


temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
}

}
printf("The sorted numbers are:"); for(i=0;i<n;i++){ printf("%3d",a[i]);
}

getch();

}
OUTPUT:
Enter the total integers you want to enter (make it less than 100):5

Enter the 5 integer array elements:

99

87

100

54

150

The sorted numbers are: 54 87 99 100 150

RESULT:

Thus a C program for the concept of bubble sort was implemented successfully.

You might also like