Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions C++/Linear search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Linear Search in C++

#include <iostream>
using namespace std;

int search(int array[], int n, int x) {

// Going through array sequencially
for (int i = 0; i < n; i++)
if (array[i] == x)
return i;
return -1;
}

int main() {
int array[] = {2, 4, 0, 1, 9};
int x = 1;
int n = sizeof(array) / sizeof(array[0]);

int result = search(array, n, x);

(result == -1) ? cout << "Element not found" : cout << "Element found at index: " << result;
}