Skip to content

Conversation

@emilyvomacka
Copy link

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sort compares each pair of adjacent items in a list and swaps them if necessary to achieve a sorted order. This process is repeated until the algorithm makes a pass through the list without exchanging any out-of-place elements.
Describe how Selection Sort works Selection sort steps through the list one item at a time, "selects" the lowest item in the unsorted portion of the list, and swaps it with the item at its current index.
Describe how Insertion sort works Insertion sort considers each element in a list and inserts it into the correct position in the previously-sorted portion of the array. The element is typically moved past all previously-sorted elements in the array and swapped repeatedly with already-sorted elements until it is correctly positioned.
Which Sorting Algorithm has the best time complexity? These algorithms are all O(n^2) time complexity (equal)
 

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 did the reverse sentence in place and did selection sort. Great job. Check out my notes and let me know if you have questions.

Comment on lines +11 to +16
while a < b do
temp = my_sentence[a]
my_sentence[a] = my_sentence[b]
my_sentence[b] = temp
a += 1
b -=1

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 twice in this method. Could you DRY this up by creating a helper method to reverse a substring?

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2), n == number of words in the sentence
# Space complexity: 1

Choose a reason for hiding this comment

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

Technically space complexity is O(n) because you are doing a .split.

# Space complexity: ?
# Time complexity: O(n^2), n == number of words in the sentence
# Space complexity: 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.

Nicely done 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