Skip to content

Conversation

@TiffanyChio
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble Sort sorts by going through an array, making comparisons between the current index and its neighbor. The larger element will be carried forward for further comparison and ultimately the largest element will be placed in the last index. The algorithm will then ignore this index (this is the sorted portion of the array) and go through this process again until all elements of an array have been sorted.
Describe how Selection Sort works Selection Sort goes through an array and looks for the smallest element. After the algorithm goes through the entire array and therefor has the minimum element, it swaps this minimum element with the element in the smallest unsorted index. The current smallest unsorted index is now sorted and will now be ignored as the algorithm sorts the rest of the array.
Describe how Insertion sort works Insertion sort traverses the array, placing the current element in the its proper place within the sorted portion of the array until all elements have been sorted.
Which Sorting Algorithm has the best time complexity? Merge Sort unless the array is already sorted, in which case the Insertion Sort has 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.

Well done. You hit all the learning goals here! You even got the reverse_sentence in place. Great work!

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

Comment on lines +10 to +12
temp = my_sentence[len - i - 1]
my_sentence[len - i - 1] = my_sentence[i]
my_sentence[i] = temp

Choose a reason for hiding this comment

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

Notice that you're reversing substrings multiple times. That makes it a great candidate for a helper method.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# 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 makes a new array and the space complexity is O(n)

# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: O(1)
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 insertion sort!

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