Skip to content

Conversation

@KKennedyCodes
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Swaps Adjacent items, until reaching the end. The largest values bubble up to the top.
Describe how Selection Sort works Uses a subarray to find the smallest value and moves it to the front. Once an item is placed to moves on to the rest of the original array until all items are in place.
Describe how Insertion sort works Insertion sort works through the array from left to right sorting from smallest to largest. As it is sorting it will actually shift the values in the array, working through the list until it reaches the end.
Which Sorting Algorithm has the best time complexity? Bubble 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, take a look at my comments regarding time complexity. Also could you reuse 1 method to do the reverse on individual words?

return string
end

def word_reverse(string)

Choose a reason for hiding this comment

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

👍 O(n)

return my_sentence
end

def string_reverse(string)

Choose a reason for hiding this comment

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

👍 O(n)

# Time complexity: ?
# Space complexity: ?
# A method to reverse the words in a sentence, in place.
# Time complexity: O(n^3)

Choose a reason for hiding this comment

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

This is actually O(n) as a solution.

Comment on lines +40 to +47
while word_start < word_end do
temp_i = string[word_start]
temp_j = string[word_end]
string[word_start] = temp_j
string[word_end] = temp_i
word_start+=1
word_end-=1
end

Choose a reason for hiding this comment

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

This does the same logic as reverse_sentence, so why not make the method flexible enough to be used in both places.

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