0

I was wondering how would I find the best, worst case of this algorithm:

Inputs 
  A: Array of Integers; N: Integer
Variables 
  i, k: Integer
Begin
  for i := N - 2 down to 0 do
    for k := 0 to i do
      if (A[k] > A[k + 1]) then
        swap(A, k, k + 1)
      fi
    od
  od
End
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
freya
  • 41
  • 1
  • 3
  • Best case as well as worst case have `O(N**2)` time complexity; the code is a *Bubble sort* implementation (https://en.wikipedia.org/wiki/Bubble_sort) – Dmitry Bychenko Jan 11 '18 at 19:54

1 Answers1

0

The worst case is when A is sorted in decreasing order. In that case the "if" statement is executed in every iteration of the nested "for".

JJxl
  • 1
  • 3