I wrote this bubble sort function but I am having a hard time figuring out the time complexity of it.
function bubbleSort(items) {
for (var i = items.length; i > 0; i--) {
for (var j = 0; j < i; j++) {
if (items[j] > items[j + 1]) {
var temp = items[j];
items[j] = items[j + 1];
items[j + 1] = temp;
}
}
}
return items;
}
I know that the outer loop has time complexity of O(n). But what is the time complexity of the inner loop (since it goes through one less element of items
on each pass) ?