Skip to content

Commit

Permalink
Answered the questions about the big O for each snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
add2point71dots committed Mar 13, 2017
1 parent 29e314e commit 84dda18
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

### Problems for you

Snippet 1 - Big O:
Snippet 1 - Big O: O(n), because in the worst case, we will iterate through the array once.
```ruby
def largest?(array, value)
array.each do |item|
Expand All @@ -24,7 +24,7 @@ def largest?(array, value)
end
```

Snippet 2 - Big O:
Snippet 2 - Big O: O(n), because while we have two each loops, they are not nested. So O(n + n) = O(n)
```ruby
def info_dump(customers)
puts "Customer Names: "
Expand All @@ -38,14 +38,14 @@ def info_dump(customers)
end
```

Snippet 3 - Big O:
Snippet 3 - Big O: O(1), because no matter how big the array is, we're only checking one element from it (the first).
```ruby
def first_element_is_red?(array)
array[0] == 'red' ? true : false
end
```

Snippet 4 - Big O:
Snippet 4 - Big O: O(n^2), because we have a nested each_with_index loop. We will be looping through the whole array for each element of the array.
```ruby
def duplicates?(array)
array.each_with_index do |item1, index1|
Expand All @@ -58,7 +58,7 @@ def duplicates?(array)
end
```

Snippet 5 - Big O:
Snippet 5 - Big O: O(n * m), because though the each loops don't depend on each other, they each run in linear time.
```ruby
words = [chocolate, coconut, rainbow]
endings = [cookie, pie, waffle]
Expand All @@ -70,7 +70,7 @@ words.each do |word|
end
```

Snippet 6 - Big O:
Snippet 6 - Big O: O(n), because we are looping through the array once.
```ruby
numbers = # some array (you don't know contents)

Expand All @@ -79,7 +79,7 @@ def print_array(array)
end
```

Snippet 7 - Big O:
Snippet 7 - Big O: O(n^2), because in the worst case, for each element we're sorting, we might have to compare it to every other element in the array again.
```ruby
# this is insertion sort
(2...num.length).each do |j|
Expand All @@ -93,7 +93,7 @@ Snippet 7 - Big O:
end
```

Snippet 8 - Big O:
Snippet 8 - Big O: O(n^2), because in the worst case, for each element we're sorting, we might have to compare it to every other element in the array again.
```ruby
# this is selection sort
n.times do |i|
Expand Down

0 comments on commit 84dda18

Please sign in to comment.