Assignment 11 CS 392 : TCPTUTOR

7
143

PROBLEM STATEMENT

Write a C program to search and element from a sorted array of integers using:
a)Linear Search b)Binary Search Algorithm

ALGORITHMS

BinarySearch(A,lower_bound,upper_bound,val)

Step 1: [Initialise]
SET BEG=lower_bound
END=upper_bound
pos=-1
Step 2: Repeat steps 3 and 4 while BEG<=END
Step 3: Set MID=(BEG+END)/2
Step 4: If A[MID]=val
Set pos=MID
Print pos
Goto step 6
Else if
A[MID]=val
Set END=MID-1
Else
Set BEG=MID+1
[End of if]
[End of Loop]
Step 5: If pos=-1
Print “value not present”
[End of if]
Step 6: EXIT
Prerequisite : Input should be sorted

LinearSearch(A,N,val)
Step 1: [Initialise]
Set pos=-1, i=0
Step 2: Repeat steps 3 and 4 while i<=N
Step 3: If A[i]=val
Set pos=i+1
Print pos
Goto step 6
[End of if]
Step 4: Set i=i+1
[End of loop]
Step 5: If pos=-1
Print “NOT FOUND”
Step 6: STOP

SOURCE CODE

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void linear(int a[],int n,int value)
{
int i,c=0;
for(i=0;i<n;i++)
{
if(a[i]==value)
{
printf(“%d is at position %d”,value,i+1);
c++;
}
}
if(c>0)
printf(“Number not found”);
}
void binary(int a[],int value,int low,int upp)
{
int mid;
mid=(upp+low)/2;
if(a[mid]==value)
printf(“%d is at position %d”,value,mid+1);
else if(value<a[mid])
binary(a,value,low,mid);
else if(value>a[mid])
binary(a,value,mid,upp);
else
printf(“Number not found”);
}
void main()
{
int a[20],n,i,value,ch;
printf(“Enter the number of elements:”);
scanf(“%d”,&n);
printf(“Enter the elements one by one:”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
while(1)
{
printf(“\nEnter the value you want to search:”);
scanf(“%d”,&value);
printf(“\n1. linear\n”);
printf(“2. Binary\n”);
printf(“3. Exit\n”);
printf(“Enter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
linear(a,n,value);
break;
case 2:
binary(a,value,0,n);
break;
case 3:
exit(0);
default:
printf(“Enter correct choice”);
}
}
}

OUTPUT

Enter the number of elements:5
Enter the elements one by one:4
1
28
7
5
Enter the value you want to search:28

1. linear
2. Binary
3. Exit
Enter your choice:1
28 is at position 3

************************

7 COMMENTS

  1. 以低能量的1064-Q激光反覆掃於皮膚表面,逐步改善皮膚色素問題。 治療時產生的熱能能促進膠原蛋白的再生,回復肌膚彈性。 使用7mm平行光光頭,使輸出的激光更溫和及均勻。功效: 去除紋身 美白及去除面毛 令整體膚色更白更均勻 減淡及去除荷爾蒙斑或反黑 增加皮膚膠原蛋白,改善皮膚彈性 減淡深層色斑 : 黃褐斑,荷爾蒙斑,蝴蝶斑 以低能量的1064-QS激光反覆掃於皮膚表面,逐步改善皮膚色素問題 使用8-10mm平行光光頭,使輸出的激光 更溫和及均勻 1. 激光能透進皮膚深層 2. 激光被黑色素吸收 3. 產生熱能將黑色素體分解 4. 被呑噬細胞吸收及排出體外

LEAVE A REPLY

Please enter your comment!
Please enter your name here