Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

What is an Algorithm?

Algorithm Specification
Linearch search:
int Linearch(const int *a, int n,int key)
{
//This returns the position of the key element if, found
// otherwise returns a 0
for(int i=0;i<n;i++)
if(a[i]=key)
return i+1;

retun 0;

}
Selection Sort
Void Selctionsort(int *a,int n)
{
for(int i=0;i<n;i++)
{
min=i;
for(int j=i+1;j<n;j++)
if(a[j]<a[min])
min=j;
if(min!=i)
{
t=a[min];
a[min]=a[i];
a[i]=t;
}
}
Binary search:
int Binarysearch(const int *a, int n,int key)
{
//The array must be in sorted order
//This returns the position of the key element if, found
// otherwise returns a 0
int low=0 , high=n-1;
While(low<=high)
{
int mid=(low+high)/2;

if(a[mid]=key)
return (mid+1);
else if(key<a[mid])
high=mid-1;// search array left to the middile element
else
low=mid+1;//search array right side to the middle element

}
return 0;
}

You might also like