You are on page 1of 2

Searching algorithm

Linear search

#include<iostream>

using namespace std;

int main() {

int n;

cout<<"Enter the number of N : ";

cin>>n;

int student[n];

int i,value,pos=-1;

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

cout<<"Number of student "<<i+1<<" = ";

cin>>student[i];

cout<<endl;

cout<<"Enter the number you want to find : ";

cin>>value;

//searching algorithm linear search

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

if(student[i]==value)

pos=i+1;

break;

if(pos==-1)

cout<<"Number not found";

else

cout<<"\nFound at Index No."<<pos;

return 0;

}
Binary search:

You might also like