Skip to content

Conversation

@M-Burr
Copy link

@M-Burr M-Burr commented Sep 25, 2019

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort works by comparing the values at neighboring/adjacent index position and swaps them such that the larger value gets pushed to the right (i.e., towards the end of the array). After the first iteration, the largest value will get pushed to the last index position; the second-to-largest value will be pushed to the second to last index position in the second iteration, and so forth.
Describe how Selection Sort works At first iteration, It searches through the entire array and finds the smallest value and inserts it at index 0. In the second iteration, it finds the second smallest value and places it in the next index position (i.e., index 1). Each iteration through the array compares from its current index position through the end of the array (i.e., not the elements at index positions which have already been sorted).
Describe how Insertion sort works It examines each index, one at a time, and the examined side is considered sorted. When examining the next (unsorted) index position, it inserts that element in the proper place of the sorted side of the array.
Which Sorting Algorithm has the best time complexity? Merge sort has the best time complexity of O(nlogn).
 

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.

# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n ^2) because lines 47-55 contain a nested loop.

Choose a reason for hiding this comment

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

Actually since start_position jumps to the end of the reversed word after a reversal, this is O(n)

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n ^2) because lines 47-55 contain a nested loop.
# Space complexity: ? O(n) because of the empty_index_positions array from lines 33 - 38

Choose a reason for hiding this comment

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

I would say O(m) where m is the number of words, but yet. Could you instead do this with O(1) space complexity?

Comment on lines +20 to +25
if my_sentence[i].length < comparison_word.length
puts my_sentence[i]
puts comparison_word
swap(my_sentence, i, j )
comparison_word = my_sentence[j]
end

Choose a reason for hiding this comment

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

This is an interesting sort, it's not one of the standard sorts but does sort elements. It does work however.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n ^ 2)
# Space complexity: ? O(n) because of splitting the string at the beginning of the method

Choose a reason for hiding this comment

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

yup

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