diff --git a/README.md b/README.md index fee3c82..4cc71f0 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,11 @@ ### Problems for you Snippet 1 - Big O: + +I would define this as 0(n) linear because the efficiency +will be directly related to how long the array is, +Since you're iterating over each item in the array. + ```ruby def largest?(array, value) array.each do |item| @@ -25,6 +30,12 @@ end ``` Snippet 2 - Big O: + +I would also define this as 0(n) linear, because, again, +it's going to depend on how long the (assumed) array of +customers is. Although we technically are iterating +over it twice, I believe we'd simplify 0(2n) to 0(n). + ```ruby def info_dump(customers) puts "Customer Names: " @@ -39,6 +50,11 @@ end ``` Snippet 3 - Big O: + +I'd say 0(1) constant, because no matter how large the +data set, since we're only checking the first element, +it's always going to take the same amount of time. + ```ruby def first_element_is_red?(array) array[0] == 'red' ? true : false @@ -46,6 +62,11 @@ end ``` Snippet 4 - Big O: + +0(n^2) quadratic because the loops are nested. +Depending on the length of the data set, the +efficiency would decrease exponentially. + ```ruby def duplicates?(array) array.each_with_index do |item1, index1| @@ -59,6 +80,13 @@ end ``` Snippet 5 - Big O: + +This one is a little confusing to me. I'd say it's +constant because the arrays of words and endings are set +with three items each, so the efficiency shouldn't change. +However if we could change those arrays, I'd say it +was 0(n*m) because the loops are nested. + ```ruby words = [chocolate, coconut, rainbow] endings = [cookie, pie, waffle] @@ -71,6 +99,14 @@ end ``` Snippet 6 - Big O: + +0(n) linear, because even though this each loop is within +a method, it doesn't affect it - it's not like +nesting two each loops. The efficiency will directly +relate to the size of the data set. I'm assuming here +that you would call the method print_array on the array +numbers. + ```ruby numbers = # some array (you don't know contents) @@ -80,6 +116,12 @@ end ``` Snippet 7 - Big O: + +Since there are nested loops - an each loop and a while +loop - I'm going to assume that this is 0(n) +exponential. The sort will take longer depending on +the length of the set for both loops. + ```ruby # this is insertion sort (2...num.length).each do |j| @@ -94,6 +136,11 @@ end ``` Snippet 8 - Big O: + +Again I'm going to assume 0(n) because of +looped statements here. The length/size of data set +affects both do/end block. + ```ruby # this is selection sort n.times do |i|