Ex. No: 6.a Linear Search Aim
Ex. No: 6.a Linear Search Aim
a LINEAR SEARCH
AIM:
To write a C program to implement the concept of linear search.
ALGORITHM:
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;
scanf("%d",&arr[i]);
if(sech==arr[i]) {
break;
getch();
}
+ TEAM
OUTPUT:
-- Linear Search --
Enter 1 element : 10
Enter 2 element : 4
Enter 3 element : 2
Enter 4 element : 17
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 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();
scanf("%d",&a[i]);
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
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(){
printf("Enter the total integers you want to enter (make it less than 100):\n");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
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
99
87
100
54
150
RESULT:
Thus a C program for the concept of bubble sort was implemented successfully.