Check your understanding of the previous reading: sorting and searching, binary search, and Insertion Sort.

Question 1

Which is faster, sorting a list of elements or searching the same list of elements?

Select one answer, then check.

  • Not quite. An iterative search checks each element at most once, while the sorting algorithms in the reading compare and move elements many times.
  • Almost. Searching is never slower, but in one case the two take about as long: when the list is already sorted, Insertion Sort only has to walk through it once, and a search for a missing item checks every element.
  • Not quite. It is the other way around: searching is usually faster.
  • Correct. An iterative search checks each element at most once, while sorting usually moves elements around many times. But on an already sorted list, Insertion Sort only walks through the list once, and a search for an item that isn't there checks every element: the two take about as long.

Question 2

True or False: time that an algorithm takes is always dependent on the size of the input, not on the actual values of the input.

Select one answer, then check.

  • Not quite. Insertion Sort from the reading is faster on an already sorted list than on a list of the same size in reverse order: the values affect the time too.
  • Correct. Insertion Sort is the counterexample: on an already sorted list it only walks through once, while in the worst case it drags every element back through the list.

Question 3

True or False: binary search would not work on an unsorted list of numbers.

Select one answer, then check.

  • Correct. After each check, binary search throws away half of the list, depending on whether the target is smaller or larger than the midpoint; that is only safe when the list is sorted.
  • Not quite. On an unsorted list, the target can be in the half that binary search throws away, so it can miss an item that is in the list.

Question 4

True or False: binary search only works on numbers, because you cannot order strings.

Select one answer, then check.

  • Not quite. Strings do support ordering comparisons: "apple" < "banana" is True, and sorted sorts a list of strings.
  • Correct. Strings support ordering comparisons (the reading says sorted works on strings too), so a sorted list of strings can be searched with binary search.

Question 5

Why is binary search faster than iterative search?

Select one answer, then check.

  • Not quite. The "binary" refers to splitting the list in two, not to binary numbers.
  • Correct. Every check throws away half of the remaining list, so a list of 1024 elements takes only about 11 checks.
  • No magic involved: every check halves the part of the list that is left.
  • Not quite. The RAM Model is a way to estimate how long a program takes, not something a search uses.
  • Not quite. That still checks elements one at a time, just in a different order, so in the worst case it checks every element.
  • The reason is well known: every check halves the part of the list that is left.

Question 6

Each time a search algorithm considers a possible item in a list, that is considered a “check”.

#QuestionChecks
1 In the best case, how many checks would iterative search need to find an item in a list of 100 numbers?
2 In the worst case, how many checks would iterative search need to find an item in a list of 100 numbers?
3 In the best case, how many checks would binary search need to find an item in a list of 100 numbers?
4 In the worst case, how many checks would binary search need to find an item in a list of 100 numbers?

Question 7

Which of the following is an accurate summary of Insertion Sort?

We recommend you check out this page: https://visualgo.net/en/sorting

Select one answer, then check.

  • Not quite. That is Selection Sort, which picks the smallest remaining element each time.
  • Correct. The elements before the current one are already in sorted order, and the inner while loop drags the current element back to its place among them.
  • Not quite. That is Bubble Sort, which swaps adjacent elements inside a nested for loop.
  • Not quite. That is MergeSort, one of the other sorts the reading lists.
  • Not quite. Shuffling until the list happens to be sorted could take forever; Insertion Sort moves each element to its place once.

Question 8

Which of the following are valid reasons that Computer Scientists study sorting and searching so much?

Select every answer that applies, then check.

  • Correct. The reading calls them two important operations on a list, useful in their own right.
  • Correct. They have been exhaustively studied, which also makes them very educational.
  • Not a valid reason. Most languages do have them built in: Python has the sorted function, for example.
  • Not a valid reason. They are studied because they are useful and educational, not to feel superior.