Hi friens
Linear search is a way of finding an element in the array that we made it . Binary search is quicker than Linear search .
in this code if key was found , it is removed from array and then sorted array is shown again.
Good Luck!
Hi friens
Linear search is a way of finding an element in the array that we made it . Binary search is quicker than Linear search .
in this code if key was found , it is removed from array and then sorted array is shown again.
Good Luck!
//Linear search
//Programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
//Date of send : 2009/2/1
#include<iostream>
#include<conio>
int linearsearch(int[],int,int);
void BubbleSort(int[],int);
void main()
{
int *a,i,k;
int n = 100;
a = new int[n];
int index,j,temp;
cout<<"Enter number (if you enter 0 , entering will be ended :\n";
cin>>temp;
for(index=0 ; temp != 0 ; index++)
{
a[index] = temp;
cin>>temp;
}
BubbleSort(a,index);
cout<<"\nTHE SORTED ARRAY IS:\n";
for(i=0 ; i<index ; ++i)
cout<<a[i]<<endl;
cout<<"\nEnter the key word:";
cin>>k;
int key = linearsearch(a,index,k);
if(key != -1) // statement of delete found number
for(j=key ; j<index ; ++j)
a[j] = a[j+1];
for(i=0 ; i<index-1 ; ++i)
cout<<a[i]<<endl;;
getch();
}
///////////////////////////////////////////////////////////////////////////////
void BubbleSort(int a[100],int y)
{
int i,j,hold;
for(i=1;i<=y-1;++i)
for(j=0;j<y-i;++j)
if(a[j]>a[j+1])
{
hold=a[j];
a[j]=a[j+1];
a[j+1]=hold;
}
}
////////////////////////////////////////////////////////////////////////////////
int linearsearch(int a[],int m,int k)
{
for(int i=0 ; i<m ; ++i)
if(a[i]==k)
return i;
return -1;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.