Sorting algorithms Flashcards

1
Q

List all sorting algorithms (3)

A
  1. Bubble sort
  2. Merge sort
  3. Quick sort

Extra: To understand how they work, watch videos. They are really helpful.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain Bubble sort

A

Start from the beginning of the list. Compare two elements. If first element is larger then the second, switch their places. Proceed to the next pair of elements and repeat until the end.

Extra: after we finish each loop, meaning we went from first element to the last, we know the last element is the largest so we don’t have to compare against him anymore. After each loop we gain another one of those, set in stone elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain Merge sort

A

Continue to divide the list in half until you are left with as many groups as there are elements. Then compare the first two elements and sort them. Continue with next two elements and so on. Then you will have groups of two elements. Join those together into groups of four and while doing that sort them. After final iteration of this method you will get sorted list.

Extra: To distinguish between Mergesort and Quicksort use the graphical representation. Remember the divide and conquer look of mergesort and it will be fine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain Quick sort

A

Start with a pivot (can be any element). On the left side of the pivot put all elements that are smaller than the pivot and on the right side put all elements that are the same or greater than pivot.

Continue this until you are left with one element groups. Then you can reconstruct the sorted list by looking at which side of the pivot the element is. Go from the bottom to the top.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Merge sort: Time complexity

A

Best case: O(nlog(n))

Worst case: O(nlog(n))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Quick sort: Time complexity

A

Base case: O(nlog(n))
Worst case: O(n^2)
Average case: O(nlog(n))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly