DEV Community

Dolly Sharma
Dolly Sharma

Posted on

Sorting for Interview

Your explanation contains the right ideas, but it is repetitive and would be difficult to present in an interview. Here's a crisp, interviewer-friendly version you can memorize.


Quick Sort

Quick Sort uses the Divide and Conquer technique.

  1. Choose a pivot element.
  2. Partition the array so that:
  • Elements smaller than the pivot go to the left.
  • Elements larger than the pivot go to the right.
    1. The pivot reaches its correct sorted position.
    2. Recursively apply the same process to the left and right subarrays.

Time Complexity

  • Best Case: O(n log n)
  • Average Case: O(n log n)
  • Worst Case: O(n²)

Why O(n log n)?

  • At each recursive level, partitioning takes O(n).
  • The array is divided recursively into approximately two halves, giving log n levels.
  • Therefore:

[
O(n) \times O(\log n) = O(n \log n)
]

Why O(n²) in the Worst Case?

If the pivot is always the smallest or largest element (for example, choosing the last element as pivot in an already sorted array):

  • First partition compares n-1 elements.
  • Second compares n-2.
  • Third compares n-3.
  • ...

Total comparisons:

[
(n-1)+(n-2)+\cdots+1
=\frac{n(n-1)}{2}
=O(n^2)
]


Merge Sort

Merge Sort also uses Divide and Conquer.

Steps:

  1. Divide the array into two halves.
  2. Continue dividing until each subarray has one element.
  3. Merge the subarrays in sorted order.

Time Complexity

  • Best: O(n log n)
  • Average: O(n log n)
  • Worst: O(n log n)

Why?

  • Dividing takes log n levels.
  • Merging at each level processes all n elements.

Therefore,

[
O(n)\times O(\log n)=O(n\log n)
]

Unlike Quick Sort, Merge Sort's worst-case complexity always remains O(n log n).


Insertion Sort

Idea: Pick one element (called the key) and insert it into its correct position in the already sorted left portion.

Algorithm:

  • Assume the first element is sorted.
  • Take the next element as the key.
  • Shift larger elements one position to the right.
  • Insert the key into its correct position.
  • Repeat for all elements.

Time Complexity

  • Best: O(n) (already sorted)
  • Average: O(n²)
  • Worst: O(n²) (reverse sorted)

Why Best Case is O(n)?

No shifting is needed; only one comparison per element.


Selection Sort

Idea: Find the minimum element from the unsorted part and swap it with the first unsorted position.

Steps:

  1. Assume the first unsorted element is minimum.
  2. Scan the remaining array.
  3. Update the minimum if a smaller element is found.
  4. Swap it with the current position.
  5. Repeat.

Time Complexity

  • Best: O(n²)
  • Average: O(n²)
  • Worst: O(n²)

Selection Sort always performs the same number of comparisons, regardless of whether the array is sorted.


Bubble Sort

Idea: Compare adjacent elements and swap them if they are in the wrong order. After each pass, the largest element "bubbles" to the end.

Time Complexity

  • Best: O(n) (with an optimization flag when no swaps occur)
  • Average: O(n²)
  • Worst: O(n²)

One-Line Interview Summary

Algorithm Best Average Worst Stable In-place
Bubble Sort O(n) O(n²) O(n²)
Selection Sort O(n²) O(n²) O(n²)
Insertion Sort O(n) O(n²) O(n²)
Merge Sort O(n log n) O(n log n) O(n log n)
Quick Sort O(n log n) O(n log n) O(n²)

This is the level of explanation expected in most technical interviews: concise, logically structured, and supported by the reasoning behind each time complexity.

Top comments (0)