Skip to main content
Ctrl+K

Laptop Lab

Mathematics

  • Figures
  • Calculus
    • Pre Calculus
      • Euclidean Trigonometry
      • Conic Sections
    • The Calculus
      • Limits
      • Derivatives
      • Integration
        • Integration
        • Techniques of Integration
        • Double Integration
        • Triple Integration
        • The Bell Curve, Gaussian Integral, & Error Function
        • HW1
      • Indeterminate Forms
      • Sequences & Series
      • Elementary Complex Analysis
    • Vector Calculus
      • Vectors
      • Dot Product
      • Vector Projection
      • Cross Product
      • Vector Functions
      • Gradient
      • Vector Fields
      • Line Integrals of Vector Fields
      • Curves & Surfaces and their Parameterizations
      • Coordinate Systems
        • Polar Coordinates
        • Cylindrical Coordinates
        • Spherical Coordinates
        • Transformations
    • Matrix Calculus
    • Tensor Calculus
    • Variational Calculus
  • Functions
    • Absolute Value
    • Boxcar Function
    • Floor and Ceiling
    • Heaviside Function
    • Ramp Function
    • Continuous Uniform Function
  • Linear Algebra
    • Introduction to Linear Systems
    • Linear Combinations
    • Homogeneity of Linear Systems
    • Linear Independence
    • Linear Transformations
    • More on Linear Transformations
    • Invertible Matrices
    • Vectors
    • Matrices
    • Some examples of the use of KaTeX
  • Algebra
    • Book: A Transition to Advanced Mathematics
  • Number Theory
    • Arithmetic
    • Fractions
    • Mathematical Induction
    • Binomial
    • Fibonacci
    • Division
    • Residues
    • Arithmetic Functions
    • Polynomials
    • Primitive Roots
    • Quadratic Residues
    • Exercises
    • Implementations
    • Book: Numbers, Groups, and Codes
      • Introduction
      • GCD & Euclid’s Algorithm
      • Congruence
      • Linear Congruences
      • 1.1
      • 1.2
      • 1.3
      • 1.4
      • 1.5
  • Logic
    • Introduction
  • Discrete Mathematics
    • Elementary Set Theory
    • Order Theory
    • Relations
    • Combinatorics
    • Formal Languages and Machines
  • Error-Correcting Codes
    • FCCT
      • Introduction to error-correcting codes
      • The main coding theory problem
      • Exercises
    • ECFF
      • Introduction & Block Codes, Weight, and Distance
      • Exercises
    • Codes
      • Repetition Codes
      • Parity Check Codes
      • Triple Check Code
      • Intro to binary codes C1, C2, C3
      • (32,64,16) Reed-Muller Code
  • Linear Programming
    • Some Definitions & Graphing
    • Convexity
    • Cones & Dual Cones
    • Row Tableaux
    • Dual LP & Sensitivity Analysis
    • Example
    • [13.2] Production Problem
    • Midterm II
  • Numerical Methods & Scientific Computing
  • More Math
    • Summations
    • Computer Algebra Systems

Computer Science & Engineering

  • Systems
    • C
    • Shell
      • Bash
    • Network Programming
    • OS
      • macOS
      • macOS Utilities
      • Windows
      • Windows Utilities
    • Distributed
      • Blockchain
      • Bitcoin
      • Ethereum
    • Hardware
    • Embedded
    • Filesystem
  • DS&A
    • Algorithms
    • Data Structures
      • Linked List
      • Stack
      • Queue
      • Priority Queue
    • Arithmetic
      • Array Sum
      • Integer Multiplication
    • Deduplication
    • Merge
      • Sorted Array Merge
      • \(k\)-Way Merge
    • Select
      • Naive Selection: \(k\)-th smallest element
      • Quickselect
    • Sort
      • Insertion Sort
      • Bubble Sort
      • Merge Sort
      • Selection Sort
      • Quick Sort
    • Search
      • Linear Search
      • Binary Search
    • Trees & Graphs
      • Trees
      • Graphs
    • Optimization
      • Linear Programming
      • Dynamic Programming
        • Lucas Numbers
        • Bellman-Ford
        • 0-1 Knapsack
    • PSU CMPSC 465
      • Assignment 1
      • Assignment 2
      • Assignment 3
      • Assignment 4
      • Assignment 5
      • Midterm
      • Assignment 6
      • Assignment 7
      • Assignment 8
      • Final
  • Programming
    • Editors
      • Vim
      • VSCode
    • Assembly
    • https://github.com/heracliteanflux/systems/tree/main/clang
    • Python
      • Packages
      • Data Science from Scratch 2e
    • R
      • Data Computing 2
      • R for Data Science 1
      • Silge & Robinson’s Text Mining with R
      • Forecasting: Principles and Practice 3
      • A01 - Tidy Data
      • A02 - Graphical Exploration
      • A04 - Data Wrangling
      • A05 - Project: Popular Names
      • A06 - Project: Bird Species
      • A07 - Project: Bicycle Sharing
      • A08 - Project: Scraping Nuclear Reactors
      • A09 - Project: Street or Road?
      • A10 - Base R
    • Go
    • D
    • Ruby
    • Notebook
    • Web
    • VR
    • Software Development
    • Documentation
    • Troubleshooting
  • Security
    • AI Sec
    • Cloud
    • Cryptography
    • CTF
      • OverTheWire
      • AOC2023
    • Defensive Sec
    • DevOps
    • Digital Forensics
    • DNS
    • GitOps
      • Git
      • GitHub
    • Hardware Sec
    • IoT
    • Malware
    • Mobile
    • Networking
      • Ethernet
      • Internet Protocol
    • OffSec
    • Passwords
    • Privacy
    • SSH
    • VMs
    • WebSec
  • Data
    • Relational
      • Postgres
      • MySQL
      • SQLite
      • Derby
    • NoSQL
    • Big Data
    • Files
    • Date Times
    • Unicode
    • Graph Data
  • Information
  • Computation
  • Artificial Intelligence
    • Robotics
    • Prompt Engineering
  • Graphics
  • Retro Gaming

Other Stuff

  • Languages & Linguistics
    • Linguistics
      • Phonetics
        • Articulatory Phonetics
        • The Acoustic Theory of Speech Production
        • Speech Processing
        • Auditory Phonetics
        • Speech Perception
        • Analysis of Speech Sounds
        • Acoustic properties of Anii vowels
        • Acoustics and audition
        • Sociophonetic properties of vowels and fricatives
        • Analysis of a stop continuum
        • Acoustic and auditory properties of speech sounds
      • Phonology
      • Morphology & Syntax
      • Semantics
      • Historical Linguistics
    • Languages
      • Constructed
      • Latin
    • Literature
      • Poetry
      • Rhetoric
      • Science Fiction
      • Tolkien
  • Philosophy
    • Ancient Philosophy
      • Ancient Near East
      • Archaic Greece
      • The Milesians
      • Anaxagoras
      • Heraclitus
      • Parmenides
      • Empedocles
      • Socrates
      • Plato
      • Aristotle
      • Atomism
      • The Megarians and Dialectic
      • Philosophical Greek
    • Medieval Philosophy
    • Early Modern Philosophy
      • Galileo
        • The Assayer (1623)
      • Hobbes
      • Descartes
        • Meditations on First Philosophy (1641)
        • Meditation I: Concerning Those Things That Can Be Called into Doubt
        • Meditation II: Concerning the Nature of the Human Mind: That It Is Better Known Than the Body
        • Meditation III: Concerning God, That He Exists
        • Meditation IV: Concerning the True and the False
        • Meditation V: Concerning the Essence of Material Things, and Again Concerning God, That He Exists
        • Meditation VI: Concerning the Existence of Material Things, and the Real Distinction between Mind and Body
        • Elisabeth’s Objection
      • Spinoza
      • Leibniz
      • Newton
      • Locke
      • Hume
      • Berkeley
      • Kant
    • Metaphysics & Ontology
    • Epistemology
    • Ethics
    • Aesthetics
    • Existentialism
    • Philosophy of Law
    • Political Philosophy and its history
    • Philosophy of History and the History of Ideas
    • Friendship and Love
    • American Philosophy
    • Chinese Philosophy
    • French Philosophy
    • Japanese Philosophy
    • Spanish Philosophy
    • History and Philosophy of Science
    • History & Philosophy of Physics & Cosmology
    • History and Philosophy of Chemistry
    • Philosophy of Biology
    • History & Philosophy of Language & Linguistics
    • Philosophy of Math
    • Philosophy of Mind & Consciousness
    • Music
  • Science
    • Astronomy & Astrophysics
      • Telescopes
    • Chemistry
    • Electrical Engineering & Electronics
      • Electromagnetism
      • Electronics
      • Electrical
    • Health & Medicine
  • Home Lab & DIY
    • 3D Print
    • Audio
    • Auto
    • Building
    • EDC
    • Game
    • IAQ
    • Metal
    • Network

Linear Search

Contents

  • Contents
  • Naive Linear Search
    • Complexity
    • Correctness
  • Linear Search
    • Complexity
    • Correctness
  • Acknowledgments

Linear Search#


Contents#

Contents

  • Linear Search

    • Contents

    • Naive Linear Search

      • Complexity

      • Correctness

    • Linear Search

      • Complexity

      • Correctness

    • Acknowledgments


Naive Linear Search#

Incremental/Iterative

 INPUT    an array A[0:n - 1] of n values where n >= 0 and a value x
OUTPUT    either an index i such that A[i] = x or the special value NOT_FOUND when A does not contain x

LINEAR_SEARCH(A, n, x)
1    result = NOT_FOUND
2    for i = 0 to n - 1
3        if A[i] == x
4            result = i
5    return result

LINEAR_SEARCH(A, n, x)
1    result = NOT_FOUND
2    i = 0
3    while i < n
4        if A[i] == x
5            result = i
6        i++
7    return result

Complexity#

Naive linear search always iterates over all \(n\) values and doesn’t ever terminate early and so its running time is \(T(n) = \Theta(n)\).

Naive linear search does not use any auxiliary data structures other than a variable to store the output value and so its space requirement is \(S(n) = \Theta(1)\).

Correctness#

Invariant

At the start of iteration \(i\), if \(\texttt{result}\) is an index then \(A[\texttt{result}] = x\). Otherwise, \(x \not\in A[0:i - 1]\).

Initialization

\(i = 0\)

Prior to executing the loop, \(\texttt{result}\) is not an index and so it is true that \(x \not\in A[0:-1]\)

Maintenance

Assume that it is true that at the start of iteration \(i\), if \(\texttt{result}\) is an index then \(A[\texttt{result}] = x\). Otherwise, \(x \not\in A[0:i - 1]\).

Then it must be shown that at the start of iteration \(i + 1\), if \(\texttt{result}\) is an index then \(A[\texttt{result}] = x\). Otherwise, \(x \not\in A[0:i]\).

CASE 1. If \(\texttt{result}\) is an index and \(A[\texttt{result}] = x\) at the start of iteration \(i\), then this continues to hold at the start of iteration \(i + 1\).

CASE 2. At the start of iteration \(i + 1\), if \(A[i] = x\) then \(\texttt{result} = i\) and \(A[\texttt{result}] = x\).

CASE 3. At the start of iteration \(i + 1\), if \(A[i] \ne x\) then \(x \not\in A[0:i - 1] \cup A[i] = A[0:i]\).

Termination

\(i = n\)

If \(\texttt{result}\) is an index then \(A[\texttt{result}] = x\). Otherwise, \(x \not\in A[0:n - 1]\).


Linear Search#

Incremental/Iterative

 INPUT    an array A[0:n - 1] of n values where n >= 0
OUTPUT    either an index i such that A[i] = x or the special value NOT_FOUND when A does not contain x

LINEAR_SEARCH(A, n, x)
1    for i = 0 to n - 1
2        if A[i] == x
3            return i
4    return NOT_FOUND

LINEAR_SEARCH(A, n, x)
1    i = 0
2    while i < n
3        if A[i] == x
4            return i
5        i++
6    return NOT_FOUND

Complexity#

Linear search iterates over \(n\) values in the worst case and so its worst case running time is \(T(n) = O(n)\). In the best case, it iterates over a single value and so its best case running time is \(T(n) = O(1)\).

Linear search does not use any auxiliary data structures other than a variable to store the output value and so its space requirement is \(S(n) = \Theta(1)\).

Correctness#

Precondition

\(A\) is an array of \(n\) values where \(n \ge 0\)

\(i = 0\)

Postcondition

\(x = A[i]\) or \(x = \texttt{NOT\_FOUND}\)

Condition

\(x \ne A[i]\) and \(i \lt n\)

Invariant

\(0 \le i \le n\)

\(x \in A[i:n-1] \cup \{ \texttt{NOT\_FOUND} \}\)

If \(x \in A\) then \(x \in A[i:n - 1]\). If \(x \not\in A[i:n - 1]\) then \(x \not\in A\).

\(x \not\in A[0:i - 1]\)

Variant

\(n - i\)

Initialization

It must be shown that if the \(\texttt{precondition}\) is true then the \(\texttt{invariant}\) is true.

Prior to the first iteration of the loop, \(i = 0\) and \(n \ge 0\) and so the inequality \(0 \le i \le n\) holds. Further it is true that \(x \in A[i:n - 1] \cup \{ \texttt{NOT\_FOUND} \}\) since \(A[i:n - 1]\) is just the entire array.

Maintenance

It must be shown that if both the \(\texttt{condition}\) is true and the \(\texttt{invariant}\) is true prior to iteration \(i\) then the \(\texttt{invariant}\) is true prior to iteration \(i' = i + 1\).

\(i \lt n \iff i + 1 \le n\)

At the start of each iteration, if \(x \in A\) then \(x \in A[i:n - 1]\).

If the algorithm does not return then \(A[i] \ne x\) and so if \(x \in A\) then \(x \in A[i + 1:n - 1]\).

Postcondition

It must be shown that if both the \(\texttt{condition}\) is false and the \(\texttt{invariant}\) is true then the \(\texttt{postcondition}\) is true.

The negation of the condition states \(\lnot(i \lt n) \iff i \ge n\) and the invariant states \(i \le n\) and so it must be the case that \(i = n\).

If \(x \not\in A[i:n - 1]\) then \(x \not\in A\).

\(\underbrace{x \not\in A[n:n - 1]}_{\text{always true}}\)

Therefore \(x \not\in A\) and so \(x = \texttt{NOT\_FOUND}\)

Termination

Up to now it has been shown that if the loop eventually terminates then the algorithm is correct. It must now be shown that the loop eventually terminates.


Acknowledgments#

02-08-2021. Algorithms Lab. “Loop Invariant Proofs (proofs, part 1)”. https://www.youtube.com/watch?v=_maJ4Qy7Q0E.


previous

Search

next

Binary Search

Contents
  • Contents
  • Naive Linear Search
    • Complexity
    • Correctness
  • Linear Search
    • Complexity
    • Correctness
  • Acknowledgments

By DF