• edited by
27,052 views
86 86 votes

The usual $\Theta(n^2)$ implementation of Insertion Sort to sort an array uses linear search to identify the position where an element is to be inserted into the already sorted part of the array. If, instead, we use binary search to identify the position, the worst case running time will

  1. remain $\Theta(n^2)$
  2. become $\Theta(n  (\log n)^2)$
  3. become $\Theta(n \log n)$
  4. become $\Theta(n)$

3 Answers

Best answer
176 176 votes

In insertion sort, with linear search, it takes

(worst case) $n$ comparisons for searching the right position, and $n$ swaps to make room to place the element.

Hence for n elements, a total of $n\times(n+n)$; $n$ for search and $n$ for swaps.

$= \Theta (2n^2) = \Theta (n^2)$

If we replace it with binary search, it takes

(worst case) $\log n$ comparisons for searching the right position, and $n$ swaps to make room to place the element.

Hence for n elements, a total of $n\times(\log n+n)$; $n$ for search and $n$ for swaps.

$= \Theta (n \times \log n + n^2) = \Theta (n^2)$

Hence, answer is A.

• edited by
1 flag:
✌ Edit necessary (Sidhant Kumar “Typing mistake In the 3rd last line, in place of " n for search" it should be " logn for search"”)
46 46 votes

A. Complexity remains same.θ(n2)

To place the element x in the correct position first we are finding its correct position in the sorted array using binary search but we have to make the space for it by shifting all elements to the right, which in worst case may be equal to the size of the sorted array.

11 11 votes

Vanilla insertion sort aur binary insertion sort dono sorting algorithms hain, lekin dono mein fark unka comparison karne ka tareeqa hai. Yeh rahi inki details:

1. Vanilla Insertion Sort

  • Working: Har element ko uski sahi position par dalte hain, pehle wale sorted part ko ek-ek karke compare karke.
  • Steps:
    • Array ka ek portion (shuru se) sorted hota hai.
    • Agla element uthao, sorted part ke saath compare karo, aur usse uski sahi jagah par insert karo.
  • Time Complexity:
    • Best Case: O(n) (Agar array already sorted ho).
    • Worst Case: O(n2)(Reverse sorted array ke liye).

2. Binary Insertion Sort

  • Working: Ismein bhi insertion sort ki tarah elements ko sorted part mein dalte hain, lekin comparison ke liye binary search ka use hota hai.
  • Steps:
    • Agla element uthao, aur uske liye sorted part mein binary search se sahi position dhoondo.
    • Us position par element insert karo.
  • Time Complexity:
    • Binary search ki wajah se comparison ka time O(logn) hota hai.
    • Best Case: O(nlog⁡n).
    • Worst Case: O(n2) (Shift karne ka cost same hai as vanilla insertion sort).

Farak:

  • Vanilla insertion sort linear search karta hai (one by one compare karta hai).
  • Binary insertion sort binary search karta hai, isliye comparison fast hoti hai, lekin shifting time dono mein same hota hai.

Dono ka use chhoti arrays ke liye theek hota hai, lekin bade data sets ke liye merge sort ya quick sort better hote hain.

Answer:
Position:
Show:

Related questions

9 9 votes
6 6 answers
3.9k
3.9k views
Arjun asked Feb 27, 2025
3,942 views
Suppose that insertion sort is applied to the array $[1,3,5,7,9,11, x, 15,13]$ and it takes exactly two swaps to sort the array. Select all possible values of $x$.$10$$12...
94 94 votes
11 answers 11 answers
31.7k
31.7k views
go_editor asked Apr 24, 2016
31,691 views
In a permutation $a_1\ldots a_n$, of $n$ distinct integers, an inversion is a pair $(a_i, a_j)$ such that $i < j$ and $a_i a_j.$What would be the worst case time complex...
61 61 votes
5 answers 5 answers
14.8k
14.8k views
Kathleen asked Sep 17, 2014
14,778 views
A program consists of two modules executed sequentially. Let $f_1(t)$ and $f_2(t)$ respectively denote the probability density functions of time taken to execute the two ...
97 97 votes
15 answers 15 answers
41.1k
41.1k views
Kathleen asked Sep 17, 2014
41,127 views
In a permutation \(a_1 ... a_n\), of n distinct integers, an inversion is a pair \((a_i, a_j)\) such that \(i < j\) and \(a_i a_j\).If all permutations are equally likel...