Skip to content

Conversation

@Kalakalot
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort works by comparing two adjacent elements in an array. If the element on the left has a higher value than the element on the right, the elements switch places. Eventually, the element with the highest value gets "bubbled" up to the top, then the element with the next-highest value, etc;
Describe how Selection Sort works Select sort is similar to the simplest method of filing a stack of (real-world) folders: the smallest element is located and placed at index 0, then the next-smallest element is located and placed at index 1, etc.
Describe how Insertion sort works Insertion sort is similar to higher-level physical filing (can you tell I used to be an administrative assistant?): elements are assessed in the order they appear in the list (as opposed to selection sort, where elements are assessed based on size, smallest first). The element at index 1 is compared to the one at index 0: if element[1] is lower, the two elements swap places. Then the element at index 2 is compared to the elements at 0 and 1 and inserted into its appropriate place in sorted list (i.e. the place where the number to the left is lower and the number to the right is higher). The algorithm continues to rotate through the unsorted portion of the list, inserting elements into the sorted portion
Which Sorting Algorithm has the best time complexity? Merge sort (O(n log n) has the best sorting algorithm time complexity, but I don't understand how "merge" works -- how does the program know where each element goes when the sub lists are merged together?
 

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.

A good start. You're definitely not finished with reverse sentence however. Take a look at my comments and let me know if this helps.

def reverse_sentence(my_sentence)
raise NotImplementedError

def reverse_sentence(test_string)

Choose a reason for hiding this comment

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

Right now you're only reversing the sentence. You then need to do another loop reversing each word. Something to work on.

Comment on lines 3 to 4
# Time complexity: ?
# Space complexity: ?

Choose a reason for hiding this comment

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

Time & space complexity?

# Time complexity: ?
# Space complexity: ?
# Time complexity: This is an insertion sort with two loops, so time complexity is O(n^2) / quadratic
# Space complexity: O(1) / constant

Choose a reason for hiding this comment

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

Technically here since you're doing .split you're making a new array of words. So this is O(n)

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