All Exams Test series for 1 year @ ₹349 only
Question

Arrange the following in ascending order of their average case complexity.

Selection-sort, Quick-sort, Linear search, Binary search

The correct answer is

Binary search, Linear search, Quick-sort, Selection-sort

Understanding Average Case Algorithm Complexity

Algorithm complexity is a measure of the resources (like time or space) an algorithm uses to solve a problem. Average case complexity describes the behavior of an algorithm on a "typical" or "random" input. It's the expected performance over many possible inputs of a given size.

Average Case Complexities of Given Algorithms

Let's look at the average case time complexities for the algorithms mentioned in the question: Selection Sort, Quick Sort, Linear Search, and Binary Search. We represent the input size as \(n\).

  • Linear Search: This algorithm checks each element one by one sequentially until it finds the target or reaches the end. On average, it has to scan about half of the elements. Its average case time complexity is \(O(n)\).
  • Binary Search: This algorithm works on a sorted array and repeatedly divides the search interval in half. This drastically reduces the number of comparisons needed. Its average case time complexity is \(O(\log n)\). Note that binary search requires the input data to be sorted.
  • Selection Sort: This sorting algorithm repeatedly finds the minimum element from the unsorted part and puts it at the beginning. It always performs a fixed number of comparisons and swaps for a given input size \(n\). Its average case time complexity is \(O(n^2)\).
  • Quick Sort: This is a divide-and-conquer sorting algorithm. It picks an element as a pivot and partitions the array around the pivot. On average, if the pivot selection is good, it performs very well. Its average case time complexity is \(O(n \log n)\).

Let's summarize these complexities in a table:

Algorithm Average Case Time Complexity
Linear Search \(O(n)\)
Binary Search \(O(\log n)\)
Selection Sort \(O(n^2)\)
Quick Sort \(O(n \log n)\)

Arranging Complexities in Ascending Order

We need to arrange these complexities from smallest to largest. For large values of \(n\), the order of growth is:

  1. \(O(\log n)\) - grows slowest
  2. \(O(n)\)
  3. \(O(n \log n)\)
  4. \(O(n^2)\) - grows fastest

Therefore, arranging the algorithms in ascending order of their average case complexity gives us:

Binary Search (\(O(\log n)\)), Linear Search (\(O(n)\)), Quick Sort (\(O(n \log n)\)), Selection Sort (\(O(n^2)\))

Comparing with Options

Let's compare our derived order with the given options:

  • Option 1: Quick-sort, Selection-sort, Binary search, Linear search (\(O(n \log n)\), \(O(n^2)\), \(O(\log n)\), \(O(n)\)) - Incorrect
  • Option 2: Linear search, Binary search, Selection-sort, Quick-sort (\(O(n)\), \(O(\log n)\), \(O(n^2)\), \(O(n \log n)\)) - Incorrect
  • Option 3: Binary search, Linear search, Quick-sort, Selection-sort (\(O(\log n)\), \(O(n)\), \(O(n \log n)\), \(O(n^2)\)) - Correct
  • Option 4: Selection-sort, Quick-sort, Linear search, Binary search (\(O(n^2)\), \(O(n \log n)\), \(O(n)\), \(O(\log n)\)) - Incorrect

The correct ascending order of average case complexities is Binary Search, Linear Search, Quick Sort, Selection Sort.

Revision Table: Algorithm Complexity Comparison

Algorithm Best Case Average Case Worst Case Notes
Linear Search \(O(1)\) \(O(n)\) \(O(n)\) Simple, no prerequisites
Binary Search \(O(1)\) \(O(\log n)\) \(O(\log n)\) Requires sorted data
Selection Sort \(O(n^2)\) \(O(n^2)\) \(O(n^2)\) Simple, in-place, stable
Quick Sort \(O(n \log n)\) \(O(n \log n)\) \(O(n^2)\) Fastest average case, unstable, space depends on recursion stack

Additional Information: Big O Notation Explained

Big O notation (\(O\)) describes the upper bound of the growth rate of a function. In the context of algorithms, it tells us how the running time or space requirements scale with the input size (\(n\)) in the worst-case or average-case scenario (depending on which complexity we are analyzing). Common Big O notations in order of increasing growth rate are:

  • \(O(1)\): Constant time, independent of \(n\).
  • \(O(\log n)\): Logarithmic time, time grows very slowly with \(n\).
  • \(O(n)\): Linear time, time grows proportionally with \(n\).
  • \(O(n \log n)\): Log-linear time, common for efficient sorting algorithms.
  • \(O(n^2)\): Quadratic time, time grows with the square of \(n\).
  • \(O(2^n)\): Exponential time, time grows very rapidly with \(n\).

Understanding Big O notation helps in comparing the efficiency of different algorithms, especially for large inputs.

Was this answer helpful?

Important Questions from Asymptotic Worst Case Time and Time Complexity

  1. What is the average case time complexity of merge sort?
  2. Arrange the following algorithms from the most efficient to least efficient based on their time complexity. 

    A. Kruskal's Algorithm

     B. Breadth first search Algorithm 

    C. Bellman-Ford Algorithm

     D. Dijkstra's Algorithm 

    E. Edmonds-Karp Algorithm. 

    Choose the correct answer from the options given below:

  3. Which of the following is correct order of increasing time complexity of algorithms

     A. Tower of Hanoi with n disk. 

    B. Binary search given n sorted numbers. 

    C. Heap sort given n numbers at the worst case. 

    D. Addition of two n x n matrices. 

    Choose the correct answer from the options given below:

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App