The first one have a n loop outside. Here n = upper - 1; And inside in every iteration it runs one less than the previous iteration. So it runs (n*(n-1))/2 = (n^2-n)/2
times. Which is O(n^2)complexity.
The second one is not a bubble sort although it's result will be the same as the first one. And it runs in O(n^2) complexity.
The first one in the first iteration puts the maximum value in arr
in the upper
th position. In the second iteration puts the second maximum value in arr
in upper-1
th postion and the third iteration put the third maximum in the upper-2
th position and so on. Each time the maximum value is put in the (last position of array - iteration number)
th postion. Hence the bubble sort.
The second one does the same goal but with different apporach. In the first iteration puts the minimum value in arr
in the 0
th position. In the second iteration puts the second minimum value in arr
in 1
th postion and the third iteration put the third maximum in the 2
nd position and so on. Each time the minimum value is put in the iteration number-1
th postion.