Sorting & Searching Questions with Answers

16 practice questions on Sorting & Searching (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 16 free in the AptiNest app.

Sample questions

Question 1Medium

The searching technique that takes O (1) time to find a data is

  1. Linear Search
  2. Binary Search
  3. Hashing
  4. Tree Search

Answer: C. Hashing

Explanation

Hash tables are often used to implement associative arrays, sets and caches. Like arrays, hash tables provide constant-time O(1) lookup on average, regardless of the number of items in the table. However, the rare worst-case lookup time can be as bad as O(n).
Compared to other associative array data structures, hash tables are most useful when large numbers of records of data are to be stored.

Step-by-step solution

  1. Hashing uses a hash function to directly locate data in constant time.
  2. The hash function maps keys to specific indices in an array, so no searching is needed.
  3. This allows data to be accessed in O(1) average time complexity.
  4. Other methods like linear or binary search require sequential checking.
  5. Tree search also involves traversing nodes and takes longer than hashing.

Why the other options are wrong

  • A. Linear Search checks each element one by one, taking O(n) time.
  • B. Binary Search works on sorted data and takes O(log n) time.
  • D. Tree Search requires traversing nodes and takes longer than hashing.

Tip: Remember: Hashing provides O(1) average time complexity for lookups.

Question 2Medium

For the quick sort algorithm, what is the time complexity of the best/worst case?

  1. best case: O(n) worst case: O(n*n)
  2. best case: O(n) worst case: O(n*log(n))
  3. best case: O(n*log(n)) worst case: O(n*log(n))
  4. best case: O(n*log(n)) worst case: O(n*n)

Answer: D. best case: O(n*log(n)) worst case: O(n*n)

Explanation

The worst case here is, if the list is already sorted in descending order and we want to sort it in ascending order. In this case it takes O(n2) time. Otherwise it takes O( n * log(n) ) time.

Step-by-step solution

  1. Quick sort splits list into two parts, each part is sorted separately.
  2. Best case: Each split divides list in half, so steps = n*log(n).
  3. Worst case: List already sorted, splits take O(n) time each step.
  4. Total worst case steps = n * n = n².
  5. Final answer: best case: O(n*log(n)) worst case: O(n*n).

Why the other options are wrong

  • A. Best case is not O(n), it's O(n*log(n)).
  • B. Worst case is not O(n*log(n)), it's O(n²).
  • C. Worst case is not O(n*log(n)), it's O(n²).

Tip: Remember: best case O(n log n), worst case O(n²).

Question 3Medium

Which of the following is not the required condition for binary search algorithm?

  1. The list must be sorted
  2. Any element of the list must be accessible in constant time
  3. There must be mechanism to delete and/or insert elements in list
  4. none of above

Answer: C. There must be mechanism to delete and/or insert elements in list

Explanation

There is no such require condition in binary search algorithm. It's not necessary that you have to add or delete any element in list.

Step-by-step solution

  1. Binary search needs a sorted list.
  2. It needs fast access to any element, such as the middle one, which an array gives in constant time.
  3. It does not need to insert or delete elements in the list.
  4. So the condition that is not required is option C. Marked answer: option C.

Why the other options are wrong

  • A. Sorted order is required, so this is a required condition.
  • B. Constant-time access is needed to check the middle element quickly, so this is required.
  • D. 'None of above' is wrong because option C is not required.

Tip: Binary search needs a sorted list and quick access to the middle; nothing about insert or delete.

Question 4Medium

Average successful search time for sequential search on 'n' items is

  1. n/2
  2. (n-1)/2
  3. (n+1)/2
  4. None of these

Answer: C. (n+1)/2

Explanation

If search key matches the very first item, with one comparison we can terminate. If it is second, two comparisons, etc.
Average =[ n (n + 1) ] / 2

Step-by-step solution

  1. Total comparisons for all positions = 1 + 2 + 3 + ... + n.
  2. Sum of first n natural numbers = n(n + 1)/2.
  3. Average comparisons = [n(n + 1)/2] / n = (n + 1)/2.
  4. So average successful search time is (n+1)/2.

Why the other options are wrong

  • A. This assumes equal probability for all items but misses the last item's contribution.
  • B. Subtracts 1 unnecessarily and doesn't account for the last element properly.

Tip: Use formula (n+1)/2 for average successful search time in sequential search.

Question 5Medium

The time taken by nondeterministic sorting algorithm is ___

  1. O(1)
  2. O(log n)
  3. O(n log n)
  4. O(n).

Answer: D. O(n).

Explanation

As there it sorts the entries in one loop. Because in nondeterministic sorting algorithms the abstractions are used which can't be programmed.

Step-by-step solution

  1. A nondeterministic sort can guess the sorted order in a single step.
  2. Checking the guessed order needs one pass over the n items, so the time is O(n).
  3. Marked answer: O(n) (option D).

Why the other options are wrong

  • A. O(1) is too small, since checking n items takes at least n steps.
  • B. O(log n) is too small for one pass over n items.
  • C. O(n log n) is the bound for deterministic comparison sorts, not for this model.

Tip: Guess-and-check sorting is O(n) in this model; deterministic comparison sorts need O(n log n).

Question 6Medium

For the bubble sort algorithm, what is the time complexity of the best/worst case? (assume that the computation stops as soon as no more swaps in one pass)

  1. best case: O(n) worst case: O(n*n)
  2. best case: O(n) worst case: O(n*log(n))
  3. best case: O(n*log(n)) worst case: O(n*log(n))
  4. best case: O(n*log(n)) worst case: O(n*n)

Answer: A. best case: O(n) worst case: O(n*n)

Explanation

Best Case is the one in which it is already sorted, and hence it takes O(n) time, otherwise it takes O(n2) time.

Step-by-step solution

  1. Bubble sort checks adjacent elements and swaps if they are out of order.
  2. In best case (already sorted), only one pass is needed with no swaps, so it takes O(n) time.
  3. In worst case (reverse sorted), it needs n-1 passes with n(n-1)/2 comparisons, which is O(n*n) time.
  4. Thus, best case is O(n) and worst case is O(n*n).
  5. Final answer: best case: O(n) worst case: O(n*n)

Why the other options are wrong

  • B. O(n*log(n)) is for merge sort, not bubble sort.
  • C. Both cases cannot be O(n*log(n)) for bubble sort.
  • D. O(n*n) is correct for worst case, but best case is not O(n*n).

Tip: Remember best case for bubble sort is O(n) as it stops early if no swaps are needed.

Question 7Medium

A machine needs a minimum of 100 sec to sort 1000 names by quick sort.The minimum time needed to sort 100 names will be approximately?

  1. 50.2 sec
  2. 6.7 sec
  3. 72.7 sec
  4. 11.2 sec

Answer: B. 6.7 sec

Explanation

In the best case quick sort algorithm makes n log(n) comparisons.

so 1000 x log (1000) = 9000 comparisons, which takes 100 sec.

To sort 100 names a minimum of 100 log(100) = 600 comparisons are needed.

This takes 100 x 600/9000 = 6.7 sec.

Step-by-step solution

  1. Quick sort's best case makes about n log n comparisons.
  2. 1000 names: 1000 × log 1000 is about 9966 comparisons, which takes 100 sec.
  3. 100 names: 100 × log 100 is about 664 comparisons, a ratio of about 0.0667 to the 1000-name work.
  4. Time for 100 names is about 100 × 0.0667 = 6.7 sec: option B.

Why the other options are wrong

  • A. 50.2 sec needs a ratio of 0.5, far above the true ratio of about 0.067.
  • C. 72.7 sec needs a ratio of 0.73, far above the true ratio of about 0.067.
  • D. 11.2 sec needs a ratio of 0.11, still above the true ratio of about 0.067.

Tip: When scaling n log n work, compare the n log n values, not just n.

Question 8Medium

The way a card game player arranges his cards as he picks them up one by one, is an example of?

  1. bubble sort
  2. selection sort
  3. insertion sort
  4. merge sort

Answer: C. insertion sort

Explanation

He scans throught the rest of the cards and pick the one with least value and places it next to the point till which he has already sorted the cards

Step-by-step solution

  1. He picks up a card and compares it with the previous ones.
  2. If it is smaller, he inserts it in the correct position.
  3. This process repeats for each new card added.
  4. The cards are arranged in order one by one.
  5. This matches insertion sort.

Why the other options are wrong

  • A. Bubble sort swaps adjacent elements repeatedly.
  • B. Selection sort finds the minimum and places it at the start.

Tip: Remember insertion sort is used for small or nearly sorted data.

Question 9Medium

Which of following algorithm scans the list by swapping the entries whenever pair of adjacent keys are out of desired order?

  1. Insertion sort
  2. Quick sort
  3. Shell sort
  4. Bubble sort

Answer: D. Bubble sort

Explanation

Bubble sort only is the algorithm from the given options which compares the adjacent keys.

Step-by-step solution

  1. Bubble sort scans adjacent keys and swaps them when they are out of order.
  2. It repeats passes until no swaps are needed.
  3. Marked answer: Bubble sort (option D).

Why the other options are wrong

  • A. Insertion sort moves each key into a sorted prefix; it is not an adjacent-swap scan.
  • B. Quick sort partitions the list around a pivot.
  • C. Shell sort compares keys a gap apart, not neighbours.

Tip: Bubble sort swaps neighbours; shell sort swaps across gaps.

Question 10Medium

What is the number of swaps required to sort n elements using selection sort, in the worst case?

  1. Θ(n)
  2. Θ(n log n)
  3. Θ(n2)
  4. Θ(n2 log n)

Answer: A. Θ(n)

Explanation

Note that we are concerned about the swaps, not the comparisons. In the best case, there is no need for any swap.
The best case scenario is when the given list of elements is already in sorted order. In the worst case, we need (n-1) swaps. For example, consider the list 10, 5, 6, 7, 8, 9.
We need 5 swaps. To conclude, 0 <= (the number of required swaps) < n

Step-by-step solution

  1. Selection sort makes at most one swap in each pass.
  2. With n elements it makes at most n - 1 swaps.
  3. The best case has 0 swaps and the worst case has n - 1 swaps, so the worst case is Θ(n). Marked answer: option A.

Why the other options are wrong

  • B. Θ(n log n) is more swaps than selection sort can make; it makes at most n - 1.
  • C. Θ(n²) is also too many; the swap count is linear, while only the comparisons are quadratic.
  • D. Θ(n² log n) is far more than the n - 1 swaps possible.

Tip: Selection sort: swaps are linear, comparisons are quadratic.

Practise all 16 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics