Time complexity in Ruby programs

Chennai.rb meet-up 25-june-2016

@h6165

Abhishek Yadav

ரூபீ ப்ரோக்ராமர்

Co-organizer: Chennai.rb

Topics covered

  • Search and binary search
  • Big O notation
  • Time complexity
  • Example-2
  • Accidentally quadratic
  • Space complexity
  • Example-3

Search and binary search


## Search an element in the given, sorted array

def search1(arr, input)
  i = 0
  while i < arr.size do
    return i if arr[i] == input
    i += 1
  end
end
  

input = 73
arr = [3, 10, 22, 72, 100, 344]

search1(73, arr)

Sequencial search

In the worst case it has to go ever the whole array

Search and binary search


## Search an element in the given, sorted array

def bsearch(arr, input, start=0, finish=arr.size-1)

  if start == finish
    return arr[start] == input ? start : nil
  end

  mid = start + (finish - start)/2

  case
  when input == arr[mid]  then return mid
  when input <  arr[mid]  then return bsearch(arr, input, start, mid)
  when input >  arr[mid]  then return bsearch(arr, input, mid+1, finish)
  end
end
  

input = 73
arr = [3, 10, 22, 72, 100, 344]

search1(73, arr)

Binary search

Search and binary search

Binary search

visualization: searching 33

Search and binary search

Binary search

  • It has to traverse a tree like path
  • In the worst case, it can be depth of the tree
  • Which is log(n) : n = array size
  • Hence: O(log n)

Big O notation

  • Time taken by algorithm is expressed as a function of size of input
  • Sequencial search: O(n)
    • For an list sized n, it may have to traverse all the elements.
  • Binary search: O(log n)
    • For a list sized n, it may have to traverse upto log n elements.
  • Selection sort: O(n^2)
    • It may have to traverse the whole list twice. Hence squared
  • ​O(1)
    • ​Constant time: doesn't depend on size of input

Selection sort

# Selection sort (very slow on large lists)

a = [9,8,6,1,2,5,4,3,9,50,12,11]
n = a.size - 1

n.times do |i|
  index_min = i

  (i + 1).upto(n) do |j|
    index_min = j if a[j] < a[index_min]
  end
  
  # Yep, in ruby I can do that, no aux variable. w00t!
  a[i], a[index_min] = a[index_min], a[i] if index_min != i
end


## Credit: https://gist.github.com/brianstorti/953310

Big O notation

  • Big-O values may be different with different datasets
  • Worst case and average case scenarios must be considered
  • Quick-sort has O(nlogn), better than others in worst case

Optimization

  • Time complexity can be sometimes improved by using space complexity
  • Use the Ruby hash

Optimization



users = User.where(active: true).to_a


## TODO

Quiz


# What is the time complexity here?

def foo(arr)

   1.upto(100).each do |i|
     1.upto(1000).each do |j|
       puts arr[i] + arr[j]
     end
   end

end

Time and space complexity with Ruby

By Abhishek Yadav

Time and space complexity with Ruby

  • 1,215