Sorting Algorithms Flashcards

1
Q
  1. Describe how the bubble sort algorithm works
  2. What is its time complexity?
A
  1. Look at com.barca.common.algorithms
  2. O(n^2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Describe how the select sort algorithm works
  2. What is its time complexity?
A
    1. Point to the first element
    2. Scan for the lowest element
    3. Swap the lowest element with the pointer element
    4. Increment the pointer by 1 and repeat 2 - 3
  1. O(n^2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Although bubble sort and select sort are both O(n^2), is one more efficient than the other?

A

Yes. Bubble sort is O(n^2) while select sort is O(n^2/2). In other words, select sort is twice as efficient as bubble sort. This is important to recognize because it asserts the importance of optimizing algorithms within the same Big O class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Describe how the insertion sort algorithm works
  2. What is its time complexity?
A
    1. Point to and “remove” the second element
    2. If the elements to the left are >, shift them to the right
    3. “Insert” the “removed” element in the space created by step 2
    4. Increment pointer and repeat 2 - 4
  1. Avergage case: O(n^2)
    Worst case: O(n^2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is an in-place sorting algorithm?

A

An in-place sorting algorithm is an algorithm that sorts data without creating any new data structures

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

What is the difference between a stable and unstable sorting algorithm?

A

A stable algorithm maintains the order of elements that have the same value. An unstable algorithm does not maintain the order of elements that have the same value

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