Skip to content

Conversation

@geli-gel
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works To bubble sort from smallest to largest, it works by looping through the list multiple times, each time comparing the current index to the one next/to the right of it, and swapping positions if the current index is greater than the one next/to the right of it. The highest item ends up at the top, having been bubbled up.
Describe how Selection Sort works It works by looping through from left to right, or from first index to last index, to find and select the lowest value in the unsorted portion (everything to the right of the current index) and swap that selected value with whatever's in the current index. It is completed at the 2nd to last iteration through the list since the last value will be the highest value.
Describe how Insertion sort works Insertion sort works by looping through from index 1 to the end, inserting whatever's at the current index to the spot it belongs in the sorted area to the left. It works as a loop from left to right as the outer loop, and a loop from whatever index the outer loop is at to index 0 (right to left), swapping positions with the values to the left if the value is greater than itself in order to insert the current index's value in between something less than and greater than itself.
Which Sorting Algorithm has the best time complexity? all of these sorting algorithms have the same time complexity of O(n^2), the sorting algorithm with the best time complexity is the O(n log n) merge sort algorithm.
 

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.

Well done. You hit the learning goals here. Take a look at my comments and let me know if you have questions.

Comment on lines +13 to +18
right = my_sentence.length - 1
until left == right || right < left
my_sentence[left], my_sentence[right] = my_sentence[right], my_sentence[left]
left += 1
right -= 1
end

Choose a reason for hiding this comment

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

Question, since you reverse things twice in this method could you dry things up a bit by making a helper method?

Comment on lines +21 to +29
regex = /([^ ]+)/
reversed_words = my_sentence.scan(regex).flatten
# positions of regex matches with enum_for code originally from "Sean Hill"'s answer at
# https://stackoverflow.com/questions/5241653/ruby-regex-match-and-get-positions-of
# gets positions of starts of each matched reversed word
reversed_word_start_indices = my_sentence.enum_for(:scan, regex).map { Regexp.last_match.begin(0) }

# for each start of word:
reversed_word_start_indices.each_with_index do |word_start_index, match_index|

Choose a reason for hiding this comment

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

Clever

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2) loop within loop, worst case same length
# Space complexity: O(1) - only creates i and j variables each time no matter input length

Choose a reason for hiding this comment

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

Technically O(n) since you are doing .split

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