Simple Algorithms Flashcards
What does binary search set as the input sequence?
The partition.
What is the first thing binary search does with the list of items within the input sequence?
Finds the index of the middle item and checks whether or not this is the target item.
How does binary search know to discard part of the input sequence each time?
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.
Does binary search operate on an ordered or an unordered input sequence?
Ordered.
What is the complexity of binary search?
O(log n).
Does bubble sort operate on an ordered or an unordered input sequence?
Either, but assume unordered.
What is the complexity of bubble sort?
O(n^2).
How many times does bubble sort iterate over a list?
n-1 times, where n is the length of the list.
What does bubble sort do on each iteration of the list?
Compares each pair of adjacent items and swaps them if they are out of order.
In the short bubble sort, what happens if no exchanges are made in an iteration?
The sort is terminated.
What is the complexity of selection sort?
O(n^2).
Does selection sort operate on an ordered or an unordered input sequence?
Either, but assume unordered.
What does selection sort do on each iteration of a list?
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 many times does selection sort iterate over a list?
n-1 times, where n is the length of the list.
By using in-place sorting in selection sort, how many lists are ‘in memory’?
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.