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 StructuresAlgorithm
Start:
- Given an integer
x
and a tableT
containingn
integers.
- Given an integer
Loop Through the Table:
- Initialize an index variable
i
to0
. - While
i
is less thann
:- If
T[i]
is equal tox
, then: - Return
True
(indicating thatx
is found in the table). - Increment
i
by1
.
- If
- Initialize an index variable
x Not Found:
- If the loop completes without finding
x
, then:- Return
False
(indicating thatx
is not found in the table).
- Return
- If the loop completes without finding
Time Complexity:
- The time complexity of this algorithm is
O(n)
, wheren
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 variablei
.