Skip to content

Commit b002077

Browse files
priydamkubdev
andauthored
Create LinearSearch.sol (#47)
* Create LinearSearch.sol adding file for Linear search. * Update src/Searches/LinearSearch.sol Co-authored-by: Max Kubik <[email protected]>
1 parent 1c88d67 commit b002077

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Searches/LinearSearch.sol

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
3+
pragma solidity >=0.7.0 <0.9.0;
4+
5+
/**
6+
* @title Checks each element of the list until a match is found or the whole list has been searched.
7+
* @author [Priyda](https://github.com/priyda)
8+
*/
9+
contract LinearSearch {
10+
11+
int result=-1;
12+
function search(uint[] memory inputData, uint searchNumber) public returns(int ) {
13+
14+
result= searchElement(inputData, searchNumber);
15+
return result;
16+
}
17+
18+
/** thislinear search will retun index of searchNumber if found else will return -1 **/
19+
20+
function searchElement(uint[] memory inputData, uint searchNumber) internal returns (int ) {
21+
22+
for(uint i=0;i<inputData.length;i++){
23+
if(inputData[i]==searchNumber){
24+
result=int(i);
25+
return result;
26+
}
27+
}
28+
29+
return result;
30+
}
31+
}

0 commit comments

Comments
 (0)