Linear Search Code

            // C++ code to linearly search x in arr[].
            // If x is present then return its location
            // otherwise return -1

            #include 
            using namespace std;

            int search(int arr[], int N, int x)
            {
              int i;
              for (i = 0; i < N; i++)
                  if (arr[i] == x)
                      return i;
              return -1;
            }

            // Driver's code
            int main(void)
            {
              int arr[] = { 2, 3, 4, 10, 40 };
              int x = 10;
              int N = sizeof(arr) / sizeof(arr[0]);

              // Function call
              int result = search(arr, N, x);
              (result == -1)
                  ? cout << "Element is not present in array"
                  : cout << "Element is present at index " << result;
              return 0;
            }
            
            # Python3 code to linearly search x in arr[].
            # If x is present then return its location
            # otherwise return -1

            def search(arr, N, x):
              for i in range(0, N):
                  if (arr[i] == x):
                      return i
              return -1


            # Driver Code
            if __name__ == "__main__":
              arr = [2, 3, 4, 10, 40]
              x = 10
              N = len(arr)

              # Function call
              result = search(arr, N, x)
              if(result == -1):
                print("Element is not present in array")
              else:
                print("Element is present at index", result)
            
            // Java code for linear searching x in arr[]            
            // If x is present then return its location
            // otherwise return -1

            class Main {
                public static int search(int arr[], int x)
                {
                  int N = arr.length;
                  for (int i = 0; i < N; i++) {
                      if (arr[i] == x)
                          return i;
                  }
                  return -1;
                }

                // Driver's code
                public static void main(String args[])
                {
                  int arr[] = { 2, 3, 4, 10, 40 };
                  int x = 10;

                  // Function call
                  int result = search(arr, x);
                  if (result == -1)
                    System.out.print("Element is not present in array");
                  else
                    System.out.print("Element is present at index " + result);
                }
            }