Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1] linear search

#include<stdio.h>

#include<conio.h>

#define size 10

int main(){

int arr[size],num,i,n,exist=0,pos=-1;

clrscr();

printf("Name:saniya jitekar");

printf("\n\nEnter no. of elements in the array : ");

scanf("%d",&n);

printf("Enter the elements :\n");

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

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

printf("\nEnter the no. to search: ");

scanf("%d",&num);

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

if(arr[i] == num){

exist = 1;

pos = i+1;

printf("%d is present in the array at position %d",num,pos);

break;

if(exist == 0){

printf("%d doesnt exist in the array",num);

getch();

return 0;
}

#include<stdio.h>

#include<conio.h>

#define size 10

int main(){

int arr[size], num, i, n, exist = 0, beg, end, mid;

clrscr();

printf("\nName:Saniya jitekar");

printf("\n\nEnter no. of elements in the array: ");

scanf("%d", &n);

printf("Enter the elements:\n");

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

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

printf("Enter the no. to search: ");


scanf("%d", &num);

beg = 0;

end = n-1;

while(beg<=end){

mid = (beg+end)/2;

if (arr[mid] == num){

printf("%d is present in array at position %d", num, mid+1);

exist = 1;

break;

else if (arr[mid]>num)

end = mid-1;

else

beg = mid+1;

if (beg > end && exist == 0)

printf("%d does not exist in the array",num);

getch();

return 0;

You might also like