Skip to content

Conversation

@dhan0406
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort works by traversing the array and comparing the adjacent elements and swapping them if they are out of order.
Describe how Selection Sort works Selection sort works by selecting the smallest unsorted item and then swapping it with the value at index 0. Then, it finds the second smallest and place it at index 1.
Describe how Insertion sort works Insertion sort works by looking at each element and inserting it into its proper place. First it assumes that the first element is sorted and it finds the next value to compare to the sorted value and then it shifts over any elements to make space for the value being added. Finally it inserts the value into the sorted subset and this repeats until the whole array is sorted.
Which Sorting Algorithm has the best time complexity? Insertion sort, bubble sort, and selection sort all have the time complexity of O(n^2).
 

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, you didn't get reverse_sentence in place, but otherwise this works well. Take a look at my comments and let me know if you have questions.

return ""
end

array = my_sentence.split(/(\S*|\s*)/) # Split by each word and each space

Choose a reason for hiding this comment

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

This is making another array causing O(n) space complexity. You were asked to do the reverse in place.

# Time complexity: O(n^2)
# 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

Comment on lines +3 to +4
# Time complexity: O(n^2)
# 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

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