Skip to content

Conversation

@north108
Copy link

@north108 north108 commented Oct 1, 2019

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, your reverse_sentence isn't done in place, but it mostly works. The sort works, but it isn't a bubble, insertion or selection sort. Take a look at my comments and let me know your questions.

end

if i == mid
my_sentence = words.join(", ")

Choose a reason for hiding this comment

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

Take a look at my how functions work lesson. Doing this doesn't affect the argument, which is why the tests fail.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) because the amount of time it will take will increase as the length of the input increases.
# Space complexity: O(n) because the space increases as the inout increases.

Choose a reason for hiding this comment

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

You were asked to try to do this in place which mean O(1) space complexity. Can you see a way?

Comment on lines +16 to +27
while index < word_array.length
if word.length < sorted[index].length
sorted.insert(index, word)
break
elsif word.length == sorted[index].length
sorted.insert(index+1,word)
break
elsif index == sorted.length-1
sorted.insert(index+1,word)
break
end
index += 1

Choose a reason for hiding this comment

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

This is a very strange nonstandard sort. It works, but does a lot of extra inserts. Take a look at the textbook curriculum lesson on sorting and compare this to the standard sorts.

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