Why is the complexity class expression this?

Click For Summary
SUMMARY

The complexity class for the provided sorting algorithm, implemented in C++, is definitively O(N²), as established by the expression 8N² + 12N + 6. This expression arises from analyzing the nested loops in the sorting function, where the outer loop runs N times and the inner loop runs approximately N/2 times on average. The coefficients in the polynomial represent the number of operations performed in the worst-case scenario, which may vary depending on the compiler's translation of the C++ code into machine language. Constants are disregarded in Big O notation as they become irrelevant as N approaches infinity.

PREREQUISITES
  • Understanding of Big O notation and complexity classes
  • Familiarity with C++ programming language
  • Knowledge of sorting algorithms and their implementations
  • Basic concepts of compiler behavior and code translation
NEXT STEPS
  • Research "Big O notation and its applications in algorithm analysis"
  • Study "C++ sorting algorithms and their complexities"
  • Explore "Compiler optimizations and their impact on performance"
  • Learn about "Empirical analysis of algorithm performance"
USEFUL FOR

Students studying computer science, software developers interested in algorithm efficiency, and educators teaching algorithm analysis and complexity theory.

Xari
Messages
3
Reaction score
0
I'm trying to understand complexity class algorithms off of my professor's lecture notes, but I still can't get a hang of it. void sort(int a[], int N) { //N is the length of the array
for (int base=0; base<N; ++base)
for (int check=base+1; check<N; ++check)
if (a[base] > a[check])
std::swap(a[base], a[check]);​
}

On his notes he says the expression for this is 8N^2+12N+6.From what I understand fully the complexity class for this is N^2 because it is the fastest growing out of the rest. We ignore constants because they're irrelevant when going to infinity.

However, I want to know how he got the coefficients.
 
Technology news on Phys.org
Xari said:
I'm trying to understand complexity class algorithms off of my professor's lecture notes, but I still can't get a hang of it. void sort(int a[], int N) { //N is the length of the array
for (int base=0; base<N; ++base)
for (int check=base+1; check<N; ++check)
if (a[base] > a[check])
std::swap(a[base], a[check]);​
}

On his notes he says the expression for this is 8N^2+12N+6.From what I understand fully the complexity class for this is N^2 because it is the fastest growing out of the rest. We ignore constants because they're irrelevant when going to infinity.

However, I want to know how he got the coefficients.
I don't think one can answer this question with the information given. It depends on how the compiler translates the C++ code into machine language.
Probably the precise 8N²+12N+6 formula was empirically derived with a given compiler for a worst case scenario.
Or maybe these values are just arbitrary. The number of instructions executed in a worst case scenario will be given by a second-degree polynomial (N for the first loop, N/2 on average for the inner loop) in N.
 
Last edited:

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
Replies
5
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K