Skip to content

Comments

leaves- Natalie Tapias#20

Open
NatalieTapias wants to merge 12 commits intoAda-C12:masterfrom
NatalieTapias:master
Open

leaves- Natalie Tapias#20
NatalieTapias wants to merge 12 commits intoAda-C12:masterfrom
NatalieTapias:master

Conversation

@NatalieTapias
Copy link

@NatalieTapias NatalieTapias commented Sep 23, 2019

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort steps through the list to be sorted and compares adjacent items, swapping them if they are in the wrong order. The algorithm continues this process until it is all the way through the list.
Describe how Selection Sort works Selection sort looks through the entire array for the smallest element, then swaps with the first element in the array. Then look for smallest element in the remaining part of the unsorted section of the array and place it in the second position. Then compare the first and second elements and swap if necessary. Continue in the same fashion for the third element until the end of the array.
Describe how Insertion sort works Insertion sort inserts each item into its proper place.
Which Sorting Algorithm has the best time complexity? Merge sort has the best time complexity, with 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.

This solves the problems, but you are not reversing the sentence in place. Consider parsing the string character-by-character and first reversing the whole string, then reversing each word using a modified version of your reversaroo method.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^3)
# Space complexity: O(n) I believe since there is a second array created in memory to store/reverse each word

Choose a reason for hiding this comment

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

Yes since you're doing .split it creates a new array, and you create a temp array.

You were asked to reverse the words in place.

end


def reversaroo(string, length)

Choose a reason for hiding this comment

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

Could this method be extended with a start and end index to reverse a substring in place?

end

length = my_sentence.length
reversaroo(my_sentence, length)

Choose a reason for hiding this comment

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

O(n)

# first fully reverse the strings

# take each 'word' and split into an array. then reversaroo the each word and store in an array called word_in_order_array
split_sentence = my_sentence.split(' ')

Choose a reason for hiding this comment

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

O(n)

# take each 'word' and split into an array. then reversaroo the each word and store in an array called word_in_order_array
split_sentence = my_sentence.split(' ')
word_in_order_array = []
split_sentence.each_with_index do |word, i|

Choose a reason for hiding this comment

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

O(m * w) where m is the number of words and w the length of the words.

end

x = 0
word_in_order_array.each do |word|

Choose a reason for hiding this comment

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

O(m)

# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^3)

Choose a reason for hiding this comment

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

The time complexity is not O(n3).

Instead you have:

O(n + n + m * w + m)

Since you will have relatively fewer words than letters (where n is the number of letters), and most words will be short, you could say this is O(n) time 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