Algorithm search
Algorithm search
Open sidebar
ChatGPT
You said:
ChatGPT said:
ChatGPT
Here are two examples of the Linear Search Algorithm written in Python and C++:
Python Example
for i in range(len(arr)):
if arr[i] == target:
# Example usage
target = 30
if result != -1:
print(f"Element found at index {result}")
else:
C++ Example
#include <iostream>
if (arr[i] == target) {
int main() {
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
return 0;
Linear search goes through each element of the array one by one.
Time Complexity:
Best Case:
Worst Case:
Space Complexity:
1
)
O(1)