Skip to content

Conversation

@gyjin
Copy link

@gyjin gyjin commented Sep 30, 2019

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort compares each pair of adjacent items and swaps them if they are in the wrong order. The process continues until it passes through the items without needing to swap anything, at which point the items should be sorted.
Describe how Selection Sort works Selection sort finds the smallest value among the items and swaps it with the item at index(0), then finds the next smallest item and swaps it with the item at index(1), and so on until all items are sorted.
Describe how Insertion sort works Insertion sort takes one item at a time from the original data structure and inserts it in the correct sorted order in another data structure.
Which Sorting Algorithm has the best time complexity? All the above sorting algorithms have the same 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, your reverse_sentence has O(n2) time complexity, and this could be revised, but it does work and it's in place!

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


while char_count < my_sentence.length
if my_sentence[0] != " "
my_sentence.insert(position_count - 1, my_sentence[0])

Choose a reason for hiding this comment

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

Just a note, that insert is an O(n) operation. Could you do this reverse_sentence in O(n) time complexity?

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2) where n is the length of string
# Space complexity: O(n) where n is the length of string

Choose a reason for hiding this comment

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

Since you're not making a new array, this is actually O(1).

# Space complexity: ?
# Time complexity: O(n^2) where n is the length of array
# Space complexity: O(n) where n is length of string/array
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 with accurate 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