Algorithms

Check each element in order until the target is found — O(n), works on unsorted data.

Version 1.2.0 · current Versions: 1.2.0 1.1.0 1.0.0

Implementations

Sign in to open in IDE Sign in with your Zerethon account to create your own copy of this sample.

Files

README.md
# Linear Search

Zerethon Official Sample `linear-search`, version 1.2.0, C (GCC 14).

- `linear_search.c` / `linear_search.h` — the algorithm, as shown in Zerethon Tools
- `main.c` — entry file: a short demonstration
- `test_linear_search.c` — checks; set it as the entry file to run them

Run compiles the entry file together with every other `.c` file except other programs
(files whose name starts with `main` or `test_`).

This project is your own copy. Changing it never changes the official sample.
See the algorithm animated: https://tools.zerethon.com/linear-search-visualizer
linear_search.c
/* Linear Search (Zerethon Official Sample: linear-search 1.2.0).
   The algorithm as shown in Zerethon Tools. */
#include "linear_search.h"

/* Returns the index of target, or -1. Works on any (unsorted) array. */
int linear_search(int a[], int n, int target) {
    for (int i = 0; i < n; i++) {
        if (a[i] == target) return i;
    }
    return -1;
}
linear_search.h
/* Linear Search (Zerethon Official Sample: linear-search 1.2.0). */
#ifndef LINEAR_SEARCH_H
#define LINEAR_SEARCH_H

int linear_search(int a[], int n, int target);

#endif
main.c entry
#include <stdio.h>
#include "linear_search.h"

static void print_array(const char *label, const int *a, int n) {
    printf("%s[", label);
    for (int i = 0; i < n; i++) printf(i ? ", %d" : "%d", a[i]);
    printf("]\n");
}

int main(void) {
    int numbers[] = {38, 27, 43, 3, 9, 82, 10, 27};
    int n = sizeof numbers / sizeof numbers[0];
    int targets[] = {43, 27, 38, 5};

    print_array("List: ", numbers, n);
    for (int i = 0; i < 4; i++) {
        int index = linear_search(numbers, n, targets[i]);
        if (index != -1) printf("search %2d: found at index %d\n", targets[i], index);
        else printf("search %2d: not found\n", targets[i]);
    }
    return 0;
}
test_linear_search.c
// Checks for linear_search. To run them, set this file as the entry file
// (the ▶ button next to it in Files) and press Run.
#include <stdio.h>
#include "linear_search.h"

static int checks = 0;

static int check(int *a, int n, int target, int expected) {
    int got = linear_search(a, n, target);
    checks++;
    if (got != expected) {
        fprintf(stderr, "linear_search(length %d, %d) = %d, expected %d\n", n, target, got, expected);
        return 0;
    }
    return 1;
}

int main(void) {
    int descending[100];
    for (int i = 0; i < 100; i++) descending[i] = 100 - i;
    int ok = 1;
    ok &= check((int[]) {0}, 0, 1, -1);
    ok &= check((int[]) {4}, 1, 4, 0);
    ok &= check((int[]) {4}, 1, 5, -1);
    ok &= check((int[]) {9, 3, 7}, 3, 9, 0);
    ok &= check((int[]) {9, 3, 7}, 3, 7, 2);
    ok &= check((int[]) {5, 1, 5, 1}, 4, 1, 1); /* the first match wins */
    ok &= check(descending, 100, 1, 99);

    if (!ok) return 1;
    printf("All %d checks passed.\n", checks);
    return 0;
}

Source: Zerethon Official Sample. Algorithm as shown in the Zerethon Tools Linear Search Visualizer.