Skip to content

Conversation

@minipaige02
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort works by looking through the array and comparing two adjacent elements. If they are out of order, it swaps the elements. It continues going through the array over and over again until all the elements in the array are in order.
Describe how Selection Sort works Selection sort first finds the minimum value in the array and swaps it will the value at index 0. Then it searches the remaining portion of the array to find the next smallest value and places at index 1. It continues to find the next smallest value in the unsorted portion of the array and places it into the next position to be filled until it reaches the end of the array.
Describe how Insertion sort works Insertion looks at each element in the array and inserts it into the correct position by comparing it to each of the elements before it. Once the element has been placed into the correct position, it moves on to the next element in the array and so on until it reaches the end of the array.
Which Sorting Algorithm has the best time complexity? Merge sort has the best time complexity - O(n log2 n).
 

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 however made a new array of words instead of reversing the sentence in place. Think about this:

  1. reverse the sentence in place
  2. Loop through the sentence
  • Then reversing each word in place (you can use a helper method that reverses the same in step 1).

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n) - my method does not have any nested loops, but it will run through the length of the original string multiple times
# Space complexity: O(n) - a second variable (reverse) needs to be created, which will take up as much space as the original string

Choose a reason for hiding this comment

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

You were asked to reverse the sentence in place. So think if you can do this with O(1) space complexity.

# sorted by the length of the word.
# 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.

Correct for a basic bubble 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