Skip to content

Commit

Permalink
Create linearsearch.c
Browse files Browse the repository at this point in the history
linear searching in C
  • Loading branch information
Raporaz03 authored Oct 2, 2023
1 parent f5963c6 commit 6af4c75
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions linearsearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 3;
int result = linearSearch(arr, n, key);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}

0 comments on commit 6af4c75

Please sign in to comment.