📰 IT NachrichtenLaut Neuzulassungen: Die meistverkauften Autos im August 2026(14.09.2026 um 20:12 Uhr)
🔧 AI Nachrichten KI-Angriffe: Geheimdienste sollen neue Befugnisse erhalten(11.09.2026 um 11:00 Uhr)
🐧 Linux TippsPACMAN: KI-Framework steuert Fusionsplasma in Echtzeit(11.09.2026 um 10:46 Uhr)
📰 IT NachrichtenLaut Neuzulassungen: Die meistverkauften Autos im August 2026(14.09.2026 um 20:12 Uhr)
🔧 AI Nachrichten KI-Angriffe: Geheimdienste sollen neue Befugnisse erhalten(11.09.2026 um 11:00 Uhr)
🐧 Linux TippsPACMAN: KI-Framework steuert Fusionsplasma in Echtzeit(11.09.2026 um 10:46 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Big O Notation and the Climb Over Constants.

↗ Quelle (dev.to)
🗣️ Stimme:

I was recently going through the implementations and complexities of common sorting algorithms such as Quicksort and Mergesort. I noticed that many programmers, including myself, often ignore the constant factors when calculating time complexity. This is typically acceptable for a high-level analysis, but there are situations where this oversight can lead to misunderstandings about the efficiency of algorithms.



To illustrate, both Quicksort and Mergesort have an average time complexity of O(n logn). Mergesort consistently achieves this complexity in all cases, making it reliable and predictable. Quicksort, on the other hand, has a worst-case time complexity of O(n²), which occurs when the pivot selection leads to highly unbalanced partitions. In these scenarios, Quicksort can perform as poorly as simple algorithms like Selection Sort. However, Quicksort is still generally considered more efficient in practice because it has an average-case time complexity of O(n logn) and tends to be faster due to its lower constant factors.



This leads to a critical question: If Quicksort has a worst-case time complexity of O(n²) and Mergesort consistently performs at O(n logn), why is Quicksort often preferred? Isn't Mergesort inherently faster?



Let's break it down. Suppose you have this simple function to print every item in an array.




CODE
void printItem1(vector<int> &arr) {
ㅤㅤ for (int i = 0; i < arr.size(); i++) {
ㅤㅤ ㅤcout << arr[i] << " ";
ㅤ ㅤ }
}






This function goes through every item in the array and prints it out. Because its iterates over the whole array once, this function runs in O(n) time. Now suppose you change this function so it sleeps for 1 second before it prints out and item:




CODE
#include <thread> // For sleep functionality
#include <chrono> // For specifying time duration
void printItem2(vector<int> &arr) {
ㅤㅤ for (int i = 0; i < arr.size(); i++) {
ㅤㅤㅤㅤ this_thread::sleep_for(chrono::seconds(1)); // Sleep for 1 second
ㅤㅤㅤㅤ cout << arr[i] << " ";
ㅤㅤ }
}






Before it prints out an item, it will pause for 1 second. Suppose you

print an array of five items using both functions.




CODE
[1, 2, 3, 4, 5]
printItem1: 1 2 3 4 5
printItem2: <sleep> 1 <sleep> 2 <sleep> 3 <sleep> 4 <sleep> 5






Both functions loop through the list once, so they're both O(n) time.

Which one do you think will be faster in practice? I think printItem1

will be much faster because it doesn't pause for 1 second before printing

an item. So even though both functions are the same speed in Big O

notation, printItems1 is faster in practice. When you write Big O

notation like O(n), it really means this,




CODE
C * n, ㅤwhere n = some amount of time.






C is some fixed amount of time that your algorithm takes. It's called the

constant. For example, it might be 10 milliseconds * n for printItem1 versus 1 second * n for printItem2.

We usually ignore that constant, because if two algorithms have

different Big O times, the constant doesn't matter. Take binary search

and simple search, for example. Suppose both algorithms had these

constants.




CODE
10ms * n for simple search
1 sec * log n for binary search






We might say, "Wow! Simple search has a constant of 10 milliseconds,

but binary search has a constant of 1 second. Simple search is way

faster!" Now suppose you're searching a list of 1 billion elements. Here

are the times.




CODE
simple search | ㅤ10ms * 1 billion = 116 days
binary search | ㅤ1s * 1 billion = 30 seconds






As you can see, binary search is still way faster. That constant didn't

make a difference at all.

But sometimes the constant can make a difference. Quicksort versus

merge sort is one example. Quicksort has a smaller constant than

merge sort. So if they're both O(n log n) time, quicksort is faster. And

quicksort is faster in practice because it hits the average case way more

often than the worst case.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
10 Quellen
GitHub Release: dependabot/dependabot-core v0.393.0 (24.08.2026)
1 Quelle
clawpatrol v0.5.10
1 Quelle
CAPE-parsers v0.1.69
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Big O Notation and the Climb Over Constants.

Thematisch verwandte Begriffe: Notation, Climb, Over, Constants · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...