Skip to content

Conversation

@raisahv
Copy link

@raisahv raisahv commented Sep 24, 2019

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble Sort goes through an array and looks at adjacent elements. If elements are out of order, they are swapped to be in order. During the first iteration, the element with the largest value is moved to the end of the array, so during the following iterations you can reduce the number of elements you are looking at by 1, because you no longer need to iterate through the last few elements of the array.
Describe how Selection Sort works Selection Sort searches an array for a minimum value and swaps the minimum value with the value at index of 0. With each iteration, Selection Sort finds the next smallest value in the array and swaps the value with the value at the next smallest index. During each iteration you can reduce the number of elements you are looking at by 1, because you no longer need to iterate through the first few elements in the array.
Describe how Insertion Sort works Insertion Sort divides an array between a portion that is sorted and a portion that is unsorted. Insertion Sort moves through an array to place items into the sorted portion of the array. During the first iteration, Insertion Sort compares values at index 0 and index 1 to find which is smallest. If the value at index 1 is smallest, you swap the values at index 0 and at index 1. During the second iteration you compare values at index 2 and index 1. If the value at index 2 is smaller than the value at index 1, the values are swapped. If the new value at index 1 is smaller than the value at index 0, the values are swapped again.
Which Sorting Algorithm has the best time complexity? Merge Sort has the best time complexity of O(nlogn). Bubble sort, Selection sort and Insertion sort all have time complexity 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.

Nice work, check out my comments and let me know if you have any questions.

# A method to reverse the words in a sentence, in place.
# Time complexity: ?
# Space complexity: ?
# Time complexity: The time complexity of this method is O(nm) where n is the length of my_sentence and m is the length of a word in my_sentence. The first while loop has a time complexity of O(1/2n) where n is the length of my_sentence. The second while loop has a time complexity of O(n)where n is the length of my_sentence and a nested while loop with a time complexity of (1/2m) where m is the length of the word within the sentence. So the total time complexity is O(3/2n * 1/2m) or O(nm).

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 word_start in the outer loop jumps to the end of a word after it reverses that word.

Comment on lines +14 to +23
while i < my_sentence.length / 2
temp_i = my_sentence[i]
temp_j = my_sentence[j]

my_sentence[i] = temp_j
my_sentence[j] = temp_i

i += 1
j -= 1
end

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 things twice in this method. Could you dry things up by making a helper method?

# sorted by the length of the word.
# Time complexity: ?
# Space complexity: ?
# Time complexity: The time complexity of this method is O(n) where n is the length of the my_sentence_array, or the number of words in the my_sentence string. The time that the algorithm takes will increase linearly with the number of words passed in.

Choose a reason for hiding this comment

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

The time complexity is O(n2). Look at the nested loops

Otherwise nice selection 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