'(Google) Can explain this code and How to find bigO notation in this code?

for(i=0;i<N-1;i=i+1)
   for(j=N-1;j!=i;j=j-1)
     if(a[j]<a[j-1])
        swap(a[j],[j-1])
  1. Can anyone explain this code does? And if (3,2,5,6,7)are inputs, what is the output?
  2. How to find the time complexity of this code(bigO Notation)?


Solution 1:[1]

  1. The code is sorting the array in the increasing order. So, if the input is (3, 2, 5, 6, 7), the output will be (2, 3, 5, 6, 7).

  2. The time complexity of the code is O(n^2) since we have two nested for loops that are going through an array of size n.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 1001pepi