Skip to content

Conversation

@dnsanche
Copy link

@dnsanche dnsanche commented Sep 23, 2019

Will answer these questions later as I need to head out!

Sorting & Reverse Sentence

Question Answer
Describe how Bubble Sort works Bubble sorts goes through the array and compares the first element with the next one, if it the first element is bigger than the second one it swaps it, then, it compares again the element swapped with the element next to it. If the the element to the right is bigger, it stops the swap and starts again from the begging of the array.
Describe how Selection Sort works It starts by defining that the current minimum is the first element and then, it goes through the whole array checking if there is another index with a lowest value. If another minimum is found, it swaps it with the current minimum. Then, the current minimum becames the values of the next index and it scans again the array to see if there is a value lower than the current minimum. If there is not, it leaves it as it is and sets the current minimum to the next index and repeats.
Describe how Insertion sort works Starts with the first element of the array (index = 0) and it compares it with the element of the left. If there is no item, it moves to next index and compares it with the elements of the left. If this element is lower, it swaps it and moves to the next index, it compares this element with the elements of the left and swaps it if it founds a bigger element until it reaches the beginning of the array. Then, moves to the next index and repeats.
Which Sorting Algorithm has the best time complexity? Merge sort is better if the size of the array is big, but if the array is small, insertions sort is faster.
 

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 the learning goals here. Nice work!

Comment on lines +2 to +3
# Time complexity: O(n), because we check at most 2 times each character.
# Space complexity: O(1), variables are always constant.

Choose a reason for hiding this comment

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

Well done

reverse(my_sentence, first ,my_sentence.length - 1)
end

def reverse(string,first,last)

Choose a reason for hiding this comment

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

Very good helper method

# Space complexity: ?
# Time complexity: O(n^2) as there are two loops that depend on the size of the elements.
# Space complexity: O(n) as I'm creating a new array.
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 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