Skip to content

Conversation

@sarashahbaig
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort take an unsorted array with very first two elements, comparing them to check which one is greater and swap them. The small elements toward the end move to the beginning slowly and the greatest element move to the end until there is no swap required.
Describe how Selection Sort works Selection Sort finds the minimum or the maximum element in an unsorted array and then puts it in its correct position in a sorted array by swapping it with index 0.
Describe how Insertion sort works In Insertion sort array elements are compared with each other sequentially  by inserting each item into its proper place to form the sorted list. It compares the current element with the largest value in the sorted array. If the current element is greater, then it leaves the element in its place and moves on to the next element else it finds its correct position in the sorted array and moves it to that position. This is done by shifting all the elements, which are larger than the current element.
Which Sorting Algorithm has the best time complexity? Merge Sort is a stable sort which means that the same element in an array maintain their original positions with respect to each other. Overall time complexity of Merge sort is O(nLogn). It is more efficient as it is in worst case also the runtime is the same.
 

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 here. Check out my comments and let me know if you have questions. Well done!


end

def reverse(sentence, i, j)

Choose a reason for hiding this comment

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

I love the helper method

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

Choose a reason for hiding this comment

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

This is actually O(n), since you don't reverse the string n times, but only smaller words. If you want to be technical it's O(n * m) where m is the length of a word, but since most words are pretty small you can drop that term.

Comment on lines +3 to +4
# Time complexity: o(n^2)
# Space complexity: o(n)

Choose a reason for hiding this comment

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

Correct

# Time complexity: o(n^2)
# Space complexity: o(n)

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.

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