Skip to content

Conversation

@rinostar
Copy link

@rinostar rinostar commented Sep 29, 2019

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Going through an unordered list, compare and swap neighbor elements if needed, until the whole list is sorted in ascending order.
Describe how Selection Sort works Find the smallest element of an unordered list and place it in the beginning of the list, then find the smallest element of the remaining list and place it in the beginning of the remaining list, and so on until reached the end of the whole list.
Describe how Insertion sort works Starting from the beginning of an unordered list, compare the new element to its left and place it in the correct location in the left, until reached the end of the whole list - same as how we would sort our cards when playing poker.
Which Sorting Algorithm has the best time complexity? All of these sorting algorithm has a O(n^2) time complexity. When n is small, insertion sort works better on average. However, when n starts to get bigger, we should start to use O(nlogn) sorting algorithm like Merge sort and Quick sort

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.

Nice work. You hit the learning goals here. Check out my comments and let me know if you have any questions.

Comment on lines 66 to 72
def pairs_helper(length)
if length % 2 == 0
pairs = length / 2
else
pairs = (length - 1) / 2
end
return pairs

Choose a reason for hiding this comment

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

I don't think this method is really needed as dividing an odd length by 2 will automatically round down.

Comment on lines 14 to 18
(sentence_pairs).times do
temp = my_sentence[i-1]
my_sentence[i-1] = my_sentence[-i]
my_sentence[-i] = temp
i += 1

Choose a reason for hiding this comment

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

Notice that you're reversing a couple of times in this method. Could you instead make a helper method. That would dry this out.

def reverse_sentence(my_sentence)
raise NotImplementedError
# Time complexity: O(n2)
# Space complexity: O(n)

Choose a reason for hiding this comment

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

Correct where n is the number of words.

# Time complexity: O(n2)
# Space complexity: O(n)

def sort_by_length(my_sentence)

Choose a reason for hiding this comment

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

Nice bubblesort

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