Skip to content

Conversation

@kristyh32
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works
Describe how Selection Sort works
Describe how Insertion sort works
Which Sorting Algorithm has the best time complexity?
 

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not bad, check out my comments and let me know if you have any questions.

@@ -1,6 +1,28 @@
# A method to reverse the words in a sentence, in place.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No comprehension question answers.

i += 1
j -= 1
end
my_sentence = reverse.join

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my lesson on how functions work This isn't changing the original argument.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
# Space complexity: O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're doing .scan you end up with O(n) because it's making a new array.

# sorted by the length of the word.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is O(n2). Check out your nested loops.

Comment on lines +9 to +19
while i < my_array.length
to_insert = my_array[i]
j = i

while j > 0 && my_array[j-1].length > to_insert.length
my_array[j] = my_array[j-1]
j -= 1
end
my_array[j] = to_insert
i += 1
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice insertion sort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants