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.cpp` / `linear_search.hpp` — the algorithm (the same steps as this sample's C code)
- `main.cpp` — entry file: a short demonstration
- `test_linear_search.cpp` — checks; set it as the entry file to run them (or Run ▾ → Run tests)

Run compiles the entry file together with every other `.cpp` 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.cpp
// Linear Search (Zerethon Official Sample: linear-search 1.2.0).
#include "linear_search.hpp"
#include <utility>

// Returns the index of target, or -1. Works on any (unsorted) vector.
int linear_search(const std::vector<int> &a, int target) {
    for (size_t i = 0; i < a.size(); i++) {
        if (a[i] == target) return static_cast<int>(i);
    }
    return -1;
}
linear_search.hpp
// Linear Search (Zerethon Official Sample: linear-search 1.2.0).
#pragma once
#include <vector>

int linear_search(const std::vector<int> &a, int target);
main.cpp entry
#include <iomanip>
#include <iostream>
#include <vector>
#include "linear_search.hpp"

int main() {
    const std::vector<int> numbers = {38, 27, 43, 3, 9, 82, 10, 27};
    std::cout << "List: [";
    for (size_t i = 0; i < numbers.size(); i++) std::cout << (i ? ", " : "") << numbers[i];
    std::cout << "]\n";
    for (int target : {43, 27, 38, 5}) {
        const int index = linear_search(numbers, target);
        std::cout << "search " << std::setw(2) << target << ": ";
        if (index != -1) std::cout << "found at index " << index << '\n';
        else std::cout << "not found\n";
    }
}
test_linear_search.cpp
// 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 <iostream>
#include <tuple>
#include <vector>
#include "linear_search.hpp"

int main() {
    std::vector<int> descending;
    for (int i = 0; i < 100; i++) descending.push_back(100 - i);
    const std::vector<std::tuple<std::vector<int>, int, int>> cases = {
        {{}, 1, -1},
        {{4}, 4, 0},
        {{4}, 5, -1},
        {{9, 3, 7}, 9, 0},
        {{9, 3, 7}, 7, 2},
        {{5, 1, 5, 1}, 1, 1}, // the first match wins
        {descending, 1, 99},
    };
    for (const auto &[a, target, expected] : cases) {
        const int actual = linear_search(a, target);
        if (actual != expected) {
            std::cerr << "linear_search(length " << a.size() << ", " << target << "): expected " << expected << ", got " << actual << '\n';
            return 1;
        }
    }
    std::cout << "All " << cases.size() << " checks passed." << std::endl;
}

Source: Zerethon Official Sample. The same algorithm as the C implementation of this sample.