Insertion sort Flashcards

(8 cards)

1
Q

What is the structured English for insertion sort?

A
  • Repeat the following for each item in the list, excluding the first
  • Remember the current item as the key
  • Move every item before the key that is bigger than it forward
  • Place the key item before these larger items
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to write an insertion sort in pseudocode?

A

for i = 1 to A.length – 1
key = A[i]
j = i - 1
while j >= 0 AND A[j] > key
A[j + 1] = A[j]
j–
endWhile
A[j + 1] = key
next i

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

Where does an insertion sort start?

A

At the second index, since the first is already sorted.

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

What is the difference between an insertion sort and a bubble sort?

A

The algorithm shifts values along. It does not swap values. (Bubble sort swaps, insertion sort shifts).

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

What are advantages of an insertion sort?

A
  • Easy to implement
  • Good for small data sets that are almost sorted
  • Insertion sort generally performs quicker than bubble sort and is therefore preferable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a disadvantage of an insertion sort?

A

It does not scale well so it’s not good for large data sets.

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

What is the worst case scenario for an insertion sort? (ascending)

A

Worst case scenario for ascending sort is when the values are in descending order

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

What is the best case scenario for an insertion sort?

A

Best case scenario is the data is already sorted

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