Using arithmetic progression sum to show an algorithm is both $\Theta(n^2)$ and $O(n^2)$

1.6k Views Asked by At

Exercise 4 in http://discrete.gr/complexity/ askes to give an arithmetic progression sum to show that the following algorithm is both $O(n^2)$ and $\Theta(n^2)$.

b = []
n.times do
  m = a[ 0 ]
  mi = 0
  a.each_with_index do |element, i|
    if element < m
      m = element
      mi = i
    end
  end
  a.delete_at( mi )
  b << m
end

sudo code:

sorted_array = []
for i in unsorted_array
  for j in unsorted_array
    if unsorted_array[j] < unsorted_array[i]
      m = unsorted_array[j]
  unsorted_array.remove(m)
  sorted_array.add(m)

I think I understand that you want to show that the progressive sum of $n, n - 1, .., n - (n - 1)$ is $\Theta(n^2)$. And I see that the worse case scenario limit approaches $n^2$, but I do not understand how to write a proof of this.

Can someone point me in the right direction.

1

There are 1 best solutions below

4
On BEST ANSWER

$$n+(n-1)+(n-2)+\cdots+1=\sum_{k=1}^nk=\frac{n(n+1)}2\in\Theta(n^2)\subset O(n^2)$$