What is the difference between sorting and searching?
Options
- Sorting orders items; searching looks for one (Correct option)
- They are the same operation under two names
- Sorting deletes items; searching adds new ones
- 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.