Write an algorithm to check whether a given integer is stored in the table.


Q.) Write an algorithm to check whether a given integer is stored in the table.

Subject: Data Structures

Algorithm

  1. Start:

    • Given an integer x and a table T containing n integers.
  2. Loop Through the Table:

    • Initialize an index variable i to 0.
    • While i is less than n:
      • If T[i] is equal to x, then:
      • Return True (indicating that x is found in the table).
      • Increment i by 1.
  3. x Not Found:

    • If the loop completes without finding x, then:
      • Return False (indicating that x is not found in the table).

Time Complexity:

  • The time complexity of this algorithm is O(n), where n is the number of integers in the table.
  • This is because the algorithm needs to check each integer in the table to determine if it is equal to the given integer x.

Space Complexity:

  • The space complexity of this algorithm is O(1).
  • This is because the algorithm does not require any additional data structures beyond the table T and the index variable i.