Skip to content

Conversation

@CEsGutierrez
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works A bubble sort evaluates two adjacent values in an array and swaps them by value, it then performs the same evaluation on an adjacent pair of values. This has the effect of allowing the high or low value to be carried along to the end of the array. It is run with two loops, the external one reducing the size of the array that is evaluated and the internal loop bubbling the values along.
Describe how Selection Sort works A selection sort makes two sections of an array, the sorted and unsorted pieces. It iterates through the entire array selects the lowest (or highest) value and then places it in the sorted portion of the array, at the start, that's probably index [0] or [-1]. It then iterates through the unsorted portion of the array, finds the next minimum value, and assigns it to the next spot in the sorted portion of the array.
Describe how Insertion sort works An insertion sort creates a sorted and unsorted part of the array, at the start, the only sorted portion is index [0]. Then, it looks to the next element in the array and assesses where to insert it relative to the value at index [0]. The sorted portion has now grown. It then selects the next value and evaluates where to insert it relative to the values in the sorted portion of the array.
Which Sorting Algorithm has the best time complexity? Insertion and Bubble sorts have the best time complexity
 

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 all the learning goals. Your reverse_sentence method could use a bit of a refactor as many of your methods are very repetitive. Other than that, excellent work.

# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(2n) The array is reviewed twice, once for spaces to populate the space array, and once more piecewise to do all the swaps

Choose a reason for hiding this comment

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

So really O(n)

Comment on lines +22 to +25
until i == half_swap_limit
my_sentence[i], my_sentence[last_index - i] = my_sentence[last_index - i], my_sentence[i]

i += 1

Choose a reason for hiding this comment

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

Notice that you reverse things a few times, this could be dryed up with a helper method.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(2n) The array is reviewed twice, once for spaces to populate the space array, and once more piecewise to do all the swaps
# Space complexity: O(1)

Choose a reason for hiding this comment

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

You are building a space array, so this would actually be: O(m) where m is the number of words.

return my_sentence

end

Choose a reason for hiding this comment

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

These methods seem pretty repetitive. Could they be condensed into one method?

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n2)
# Space complexity: O(1)

Choose a reason for hiding this comment

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

Technically since you're doing .split, this should be O(n)


if array_of_sentence[i].length > array_of_sentence[i + 1].length
array_of_sentence[i], array_of_sentence[i + 1] = array_of_sentence[i + 1], array_of_sentence[i]
i += 1

Choose a reason for hiding this comment

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

You could move the i += 1 to after the if statement and drop the else.

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