Skip to content

Commit

Permalink
Create linear_search.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Darsh-k-Shah authored Oct 1, 2020
1 parent 8fb6ffc commit 4b70f1e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions linear_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<bits/stdc++.h>
using namespace std;

/**
* Algorithm implementation
* \param [in] array array to search in
* \param [in] size length of array
* \param [in] key key value to search for
* \returns index where the key-value occurs in the array
* \returns -1 if key-value not found
*/
int main()
{
vector <int> v;
int key,size;
int flag = 0;

// Input array
cout<<"Enter number of elements\n";
cin>>size;
cout << "\nEnter elements of the Array to be searched which is to be searched";
for (int i = 0; i < size; i++)
{
cin >> key;
v.push_back(key);
}

cout << "\nEnter the number to be searched : ";
cin >> key;

for(int i = 0;i<v.size();i++)
{
if(v[i] == key)
{
flag = 1;
cout<<"Found at position: "<<i + 1<<"\n";
break;
}
}

if (flag == 0)
cout << "Number not found\n";
return 0;
}

0 comments on commit 4b70f1e

Please sign in to comment.