Skip to content

Conversation

@brikemp
Copy link

@brikemp brikemp commented Sep 29, 2019

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort swaps items that are next to one another if they are out of order and leaves them where they are if they are in the correct order. It starts at the beginning of the array with the first two elements and continues until it gets to the end.
Describe how Selection Sort works Selection sort iterates through an array to find the smallest element and swaps it with the first element in the unsorted portion of the array
Describe how Insertion sort works Insertion sort uses two arrays (unsorted and sorted) and moves items from the unsorted array to its appropriate position in the sorted array.
Which Sorting Algorithm has the best time complexity? Merge sort - O(n log n)
 

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. Well done. Take a look at my comments and let me know if you have any questions.

Comment on lines +10 to 18
def reverse(text, text_start, text_end)
i = 0
((text_end - text_start)/2).times do
temp_char = text[text_start + i]
text[text_start + i] = text[text_end - i - 1]
text[text_end - i - 1] = temp_char
i += 1
end
end

Choose a reason for hiding this comment

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

Great helper method.

Could you have used it in other places in the main method?

# Space complexity: ?
def reverse_sentence(my_sentence)
raise NotImplementedError
# Time complexity: O(n + m) -

Choose a reason for hiding this comment

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

You're reversing the whole string = n
Then you go through and reverse each word for a total of < n letters reversed
So O(2n) = O(n)

Comment on lines +27 to +32
while letter < my_sentence.length/2 do
temp_char = my_sentence[letter]
my_sentence[letter] = my_sentence[length - 1 - letter]
my_sentence[length - 1 - letter] = temp_char
letter += 1
end

Choose a reason for hiding this comment

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

Hmm... another reversal section

end
end
my_sorted_array.push(current_word)
my_array.delete(current_word)

Choose a reason for hiding this comment

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

Just note .delete is an O(n) operation.

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