for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++){
// do swap stuff, constant time
}
}
I read that single for loop is O(N)
and traversing it twice will make it O(n^2)
.
I watched related tutorials which shows that each operation takes unit of 1 - O(1)
. I would like see in details how O(n^2)
actually came up. I tried to do math for each statement but I believe I am not doing it right. I would appreciate if someone can literally show me how nested for loop becomes O(n^2)
. Thanks beforehand