Standard Algorithms: Search, Linear and Binary Search, Sorting, Bubblesort, Selection, and Insertion
Searching and sorting are among the most fundamental operations in software development. In this article, you’ll learn five essential standard algorithms: linear search, binary search, bubblesort, selection sort, and insertion sort. For each algorithm, I’ll explain how it works, its time complexity in Big-O notation, and provide a Python example.
Whether you’re preparing for an exam, pursuing a degree, or undertaking any IT training, searching and sorting will inevitably come up. Usually it’s just the basics—simple bubblesort implementations or basic array operations—but understanding these algorithms matters.
Learning the key algorithms will make your life easier. Time is money, and this is where the first dilemma in programming appears: when is an algorithm good, and when is it unusable?
The larger your dataset, the more important it is to choose the right approach. With small datasets, the difference between waiting 0.2 seconds or 0.5 seconds feels negligible. But with large datasets, that gap can stretch from seconds to minutes.
We measure algorithms by speed. The goal is always to find the fastest solution. We’ve written about this before, but since it’s relevant here, let me recap the essentials.
What do O(1), O(n), O(log n), and O(n²) mean?
Before we look at individual algorithms, you need to understand how we describe their speed using Big-O notation. It doesn’t tell you how many milliseconds an algorithm takes; rather, it describes how much the runtime grows as your data grows.
Here’s a quick overview of the main complexity classes:
- O(1) – constant: Runtime stays the same regardless of data size. Accessing an array element by index is an example.
- O(log n) – logarithmic: Runtime grows very slowly. Binary search halves the search space each step, which is why it’s O(log n).
- O(n) – linear: Runtime grows in proportion to your data size. With 1,000 elements, you’ll need roughly twice as long as with 500 elements.
- O(n²) – quadratic: Runtime grows very quickly. When you double the data, runtime increases about four times. Many simple sorting algorithms have this complexity.
Big-O notation always describes the worst case—the most unfavorable scenario. This helps you estimate the worst-case runtime your algorithm might face.
For a deeper explanation, check out Big-O Notation: Runtime Complexity and Efficiency O(1), O(n), O(log n) and the introduction to Complexity Analysis, Big-O, Search and Sorting Algorithms.
For the algorithms below, it’s enough to look at our summary and judge each as either “GOOD” or “NOT SO GOOD.”
Linear Search
Linear search moves through a list element by element until it finds the target or reaches the end. It works on both sorted and unsorted data.
Think of it like walking through a queue from the start to the end. You might get lucky and find what you need right away, or it could be the last element.
def lineare_suche(liste, ziel):
for index, wert in enumerate(liste):
if wert == ziel:
return index
return -1
- Best Case: O(1) (target element is near the front)
- Worst Case: O(n) (target element is at the end, or doesn’t exist)
- Average Case: O(n)
Linear search is simple but slow on large datasets.
Binary Search
Binary search halves the search space with each step. It requires the list to be sorted first. Compared to linear search, it’s dramatically faster on large datasets.
def binaere_suche(liste, ziel):
links = 0
rechts = len(liste) - 1
while links <= rechts:
mitte = (links + rechts) // 2
if liste[mitte] == ziel:
return mitte
if liste[mitte] < ziel:
links = mitte + 1
else:
rechts = mitte - 1
return -1
- Best Case: O(1)
- Worst Case: O(log n)
- Average Case: O(log n)
With a million elements, binary search needs only about 20 comparisons in the worst case.
Compare that to linear search—see the difference?
Bubblesort
Bubblesort repeatedly compares adjacent elements and swaps them if they’re in the wrong order. Larger elements gradually bubble up toward the end.
This algorithm is popular in exams and introductory exercises. You should understand it and be able to implement it later.
def bubblesort(liste):
n = len(liste)
while True:
vertauscht = False
for i in range(n - 1):
if liste[i] > liste[i + 1]:
liste[i], liste[i + 1] = liste[i + 1], liste[i]
vertauscht = True
n -= 1
if not vertauscht:
break
return liste
- Best Case: O(n)
- Worst Case: O(n²)
- Average Case: O(n²)
Bubblesort is easy to understand but inefficient for large datasets.
Selection Sort
Selection sort finds the smallest element in the unsorted portion during each pass and moves it to the sorted portion.
def selection_sort(liste):
n = len(liste)
for i in range(n - 1):
min_index = i
for j in range(i + 1, n):
if liste[j] < liste[min_index]:
min_index = j
liste[i], liste[min_index] = liste[min_index], liste[i]
return liste
- Best Case: O(n²)
- Worst Case: O(n²)
- Average Case: O(n²)
Selection sort is straightforward but always quadratic. It performs fewer swaps than bubblesort.
Insertion Sort
Insertion sort places each element into its correct position within the already-sorted portion. It’s like sorting cards in your hand during a card game.
def insertion_sort(liste):
for i in range(1, len(liste)):
aktuelles = liste[i]
j = i - 1
while j >= 0 and liste[j] > aktuelles:
liste[j + 1] = liste[j]
j -= 1
liste[j + 1] = aktuelles
return liste
- Best Case: O(n)
- Worst Case: O(n²)
- Average Case: O(n²)
Insertion sort is particularly efficient when the list is already nearly sorted.
Comparison of Search and Sorting Algorithms
| Algorithm | Type | Best Case | Worst Case | Use Case |
|---|---|---|---|---|
| Linear Search | Search | O(1) | O(n) | Small unsorted datasets |
| Binary Search | Search | O(1) | O(log n) | Large sorted datasets |
| Bubblesort | Sorting | O(n) | O(n²) | Learning purposes only |
| Selection Sort | Sorting | O(n²) | O(n²) | Small datasets, minimizing swaps |
| Insertion Sort | Sorting | O(n) | O(n²) | Nearly sorted data, small lists |
Which Algorithm to Use When?
For searching:
- Use linear search for unsorted data.
- Use binary search for sorted data.
For sorting:
- Bubblesort is suitable only for learning.
- Selection Sort is straightforward but slow.
- Insertion Sort works well for small or nearly sorted datasets.
- For large datasets, reach for Quicksort, Mergesort, or Heapsort instead.
Linear search, binary search, Bubblesort, Selection Sort, and Insertion Sort form the foundation for understanding algorithms and data structures. They’re simple, instructive, and demonstrate how Big-O describes runtime complexity. In production, larger datasets call for faster algorithms, but mastering these five standard algorithms is essential for any developer.
Recommended Reading on Algorithms and Data Structures
To deepen your knowledge of algorithms and data structures, we recommend the following books:
Algorithms & Data Structures
Books about algorithms, complexity analysis, data structures and algorithmic security
Introduction to Algorithms von Thomas H. Cormen u.a.
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.
Grokking Algorithms, Second Edition von Aditya Y. Bhargava
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.
FAQ: Standard Algorithms, Searching, and Sorting
1. What is a standard algorithm?
2. What is the difference between searching and sorting?
3. What is linear search?
4. When should you use linear search?
5. What is binary search?
6. Why is binary search faster than linear search?
7. What prerequisite does binary search need?
8. What is Bubblesort?
9. Why is Bubblesort rarely used in practice?
10. What is Selection Sort?
11. What is Insertion Sort?
12. What is the best case for Insertion Sort?
13. What does O(1) mean?
14. What does O(n) mean?
15. What does O(log n) mean?
16. What does O(n²) mean?
17. What is the worst case?
Don’t worry—we have an article on binary search as well. After reviewing these FAQs, you already understand how it works.




