File tree 1 file changed +31
-0
lines changed
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments