Linear Search in Array

Code

#include <stdio.h>
#include <stdint.h>

int linear_search(uint8_t *arr, uint8_t n, uint8_t key) {
    for (uint8_t i = 0; i < n; i++) {
        if (arr[i] == key) {
            return i;  // Found at index i
        }
    }
    return -1;  // Not found
}

int main() {
    uint8_t n, key;
    scanf("%hhu", &n);
    uint8_t arr[100];

    for (uint8_t i = 0; i < n; i++) {
        scanf("%hhu", &arr[i]);
    }
    scanf("%hhu", &key);

    int index = linear_search(arr, n, key);
    printf("%d", index);
    return 0;
}

Solving Approach

Here’s a clear and embedded-style solving algorithm for performing a linear search on a uint8_t array:

🪜 Step-by-Step Solving Algorithm

  1. Input
    • Read n: number of elements in the array
    • Read arr[]: array of n unsigned 8-bit integers
    • Read key: the value to search for
  2. Initialize
    • Set index = -1 to represent "not found"
  3. Loop through the array
    • For each index i from 0 to n - 1:
      • If arr[i] == key:
        • Set index = i
        • Break the loop (first occurrence found)
  4. Output
    • Print index (either the found index or -1)

🧪 Example

Input:

n = 5  
arr = [10, 20, 30, 40, 50]  
key = 30

Output:

2

 

 

 

Upvote
Downvote
Loading...

Input

5 10 20 30 40 50 30

Expected Output

2