mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge pull request #79 from hegdenaveen1/patch-4
Rectified Linear Search
This commit is contained in:
commit
ceb6a8eff7
@ -1,13 +1,13 @@
|
|||||||
#include<iostream>
|
#include<iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int LinearSearch(int *array, int key)
|
int LinearSearch(int *array, int size, int key)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < size; ++i)
|
||||||
{
|
{
|
||||||
if (array[i]==key)
|
if (array[i]==key)
|
||||||
{
|
{
|
||||||
return key;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,19 +17,24 @@ int LinearSearch(int *array, int key)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int array[10];
|
int size;
|
||||||
|
cout<<"\nEnter the size of the Array : ";
|
||||||
|
cin >> size;
|
||||||
|
|
||||||
|
int array[size];
|
||||||
int key;
|
int key;
|
||||||
|
|
||||||
//Input array
|
//Input array
|
||||||
cout<<"\nEnter the Array of 10 numbers : ";
|
cout<<"\nEnter the Array of " << size << " numbers : ";
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
cin>>array[i];
|
cin>>array[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
cout<<"\nEnter the number to be searched : ";
|
cout<<"\nEnter the number to be searched : ";
|
||||||
cin>>key;
|
cin>>key;
|
||||||
|
|
||||||
int index=LinearSearch(array, key);
|
int index=LinearSearch(array, size, key);
|
||||||
if (index!=-1)
|
if (index!=-1)
|
||||||
{
|
{
|
||||||
cout<<"\nNumber found at index : "<<index;
|
cout<<"\nNumber found at index : "<<index;
|
||||||
|
Loading…
Reference in New Issue
Block a user