Simple Algorithms Flashcards

1
Q

What does binary search set as the input sequence?

A

The partition.

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

What is the first thing binary search does with the list of items within the input sequence?

A

Finds the index of the middle item and checks whether or not this is the target item.

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

How does binary search know to discard part of the input sequence each time?

A

As the input sequence is ordered, binary search compares the target item with the middle item of the input sequence - if the target item is greater, then the lower half of the input sequence can be discarded and vice versa.

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

Does binary search operate on an ordered or an unordered input sequence?

A

Ordered.

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

What is the complexity of binary search?

A

O(log n).

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

Does bubble sort operate on an ordered or an unordered input sequence?

A

Either, but assume unordered.

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

What is the complexity of bubble sort?

A

O(n^2).

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

How many times does bubble sort iterate over a list?

A

n-1 times, where n is the length of the list.

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

What does bubble sort do on each iteration of the list?

A

Compares each pair of adjacent items and swaps them if they are out of order.

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

In the short bubble sort, what happens if no exchanges are made in an iteration?

A

The sort is terminated.

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

What is the complexity of selection sort?

A

O(n^2).

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

Does selection sort operate on an ordered or an unordered input sequence?

A

Either, but assume unordered.

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

What does selection sort do on each iteration of a list?

A

Compares the element at index i with each element in the list, swapping the element at index i with the first lesser element it finds, then increases the index before iterating again.

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

How many times does selection sort iterate over a list?

A

n-1 times, where n is the length of the list.

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

By using in-place sorting in selection sort, how many lists are ‘in memory’?

A

Two, a sorted list to the left of the element at index i, and an unsorted list to the right of the element at index i.

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

What is the complexity of insertion sort?

A

O(n^2).

17
Q

Does insertion sort operate on an ordered or an unordered input sequence?

A

Either, but assume unordered.

18
Q

How many times does insertion sort iterate over a list?

A

1 time.

19
Q

What does insertion sort do on an iteration of a list?

A

Compares the element at index i with the next element in the list, the lesser of the two is considered sorted. The algorithm then compares the next element at index i with the element at index i+1, the lesser of the two is then compared to the element at i-1, and the list is sorted backwards.

20
Q

By using in-place sorting in insertion sort, how many lists are ‘in memory’?

A

Two, a sorted list to the left of the element at index i, and an unsorted list to the right of the element at index i.