Repaso
EspaƱol
Algorithms & DSFoundationsALG-56

What is the difference between sorting and searching?

Options

  1. Sorting orders items; searching looks for one (Correct option)
  2. They are the same operation under two names
  3. Sorting deletes items; searching adds new ones
  4. Searching only works on data that is sorted

Answer

Sorting puts the items in a defined order; searching finds a particular item (or checks if it exists)

Why

Sorting: [3, 1, 2] becomes [1, 2, 3].

Searching: is 2 in the list, and where? On an unsorted list you check items one by one (linear search, O(n)). On a sorted list you can use binary search (O(log n)).

This is why sorting first can pay off when you search many times.

Remember

Sorting orders. Searching finds.

Key terms

Practice this question in Repaso