При оценке Big O алгоритма, начните с самого внутреннего блока, оцените его сложность и выйдите.
for (int i = 1; i < peopleList.size(); i++) { // Will always iterate through the entire list (O(n))
for (int j = i; j > 0; j--) { // Best case, this iterates once per outter loop (constant), worst case iterates n times (O(n))
Person lower = peopleList.get(j - 1); //constant operation (assuming ArrayList)
Person higher = peopleList.get(j); //constant operation (assuming ArrayList)
if (higher.name.compareTo(lower.name) < 0) { //constant operation
peopleList.set(j, lower); //constant operation
peopleList.set(j - 1, higher); //constant operation
} else {
break;
}
}
}
}
Затем вы умножите сложность всех слоев циклов.И вы получите лучший случай O (n) и худший случай O (n ^ 2)