Skip to content

Conversation

@dtaylor73
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort sorts an array of numbers by gradually pushing the largest number to the top
Describe how Selection Sort works Selection Sort looks through the entire array for the smallest element. Once found, it is swapped with the first element of the array.
Describe how Insertion sort works Insertion sort looks through an array of numbers, finds the smallest number and then places it in its correct spot
Which Sorting Algorithm has the best time complexity? Merge-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.

Outstanding work! You hit all the learning goals here. Very well done!

Take a look at my comments and let me know if you have questions.

Comment on lines +50 to +65
def word_reverse(my_sentence, word_start, word_end)
word_length = word_end - word_start + 1

counter = 0

while counter != word_length/2
temp = my_sentence[word_start]
my_sentence[word_start] = my_sentence[word_end]
my_sentence[word_end] = temp

word_start += 1
word_end -= 1
counter += 1
end
end

Choose a reason for hiding this comment

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

I love this helper method! 👍

Comment on lines +39 to +44
until i == my_sentence.length/2
temp = my_sentence[i]
my_sentence[i] = my_sentence[my_sentence.length - i - 1]
my_sentence[my_sentence.length - i - 1] = temp
i += 1
end

Choose a reason for hiding this comment

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

You could also use the word_reverse method again to reverse the whole string.

# Space complexity: ?
# Time complexity: O(n^2) because as the algorithm scales, each nested loop will run n number of times according to the input.
# Space complexity: O(1) because the same amount of memory will be used regardless of the input size.
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 SmartBubble sort!

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2) because as the algorithm scales, each nested loop will run n number of times according to the input.
# Space complexity: O(1) because the same amount of memory will be used regardless of the input size.

Choose a reason for hiding this comment

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

Technically since you are doing .split, you have O(n) space complexity.

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