0% found this document useful (0 votes)
6 views4 pages

Unit1 Insertion Sort KCS503

The document provides an overview of the Insertion Sort algorithm, detailing its pseudo code and the analysis of its running time in both best and worst-case scenarios. The best case occurs when the array is sorted, resulting in linear growth, while the worst case, when the array is reverse sorted, leads to quadratic growth. It emphasizes the importance of worst-case analysis for understanding algorithm efficiency and introduces the concept of order of growth.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Unit1 Insertion Sort KCS503

The document provides an overview of the Insertion Sort algorithm, detailing its pseudo code and the analysis of its running time in both best and worst-case scenarios. The best case occurs when the array is sorted, resulting in linear growth, while the worst case, when the array is reverse sorted, leads to quadratic growth. It emphasizes the importance of worst-case analysis for understanding algorithm efficiency and introduces the concept of order of growth.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

KIET Group of Institutions, Ghaziabad

Department of Computer Applications


(An ISO – 9001: 2015 Certified & ‘A’ Grade accredited Institution by NAAC)
Data Structure
RCA 303: Session 2020-21

Unit-1 Lecture-3 Topic: Insertion Sort


Video Link

Before going for growth of functions and asymptotic notation let us see how to analyze an
algorithm.
How to analyze an Algorithm
Let us form a first algorithm, insertion sort, solves the sorting problem((which sort a sequence
of numbers).
The pseudo code for the algorithm is give below.
Pseudo code:
INSERTION-SORT(A)
1 for j ← 2 to length[A]
2 do key ← A[ j ]
3 ✄ Insert A[ j ] into the sorted sequence A[1 . . j − 1].
4 i←j−1
5 while i > 0 and A[i ] > key
6 do A[i + 1] ← A[i ]
7 i ←i − 1
8 A[i + 1] ← key

Example

Compiled by: Prashant Agrawal, Associate Professor


KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2015 Certified & ‘A’ Grade accredited Institution by NAAC)
Data Structure
RCA 303: Session 2020-21

Analysis of Algorithm

Let us start by presenting the INSERTION-SORT procedure with the time “cost” of each
statement and the number of times each statement is executed. For each j = 2, 3, . . . , n, where
n = length[A], we let t j be the number of times the while loop test in line 5 is executed for that
value of j. When a for or while loop exits in the usual way (i.e., due to the test in the loop
header), the test is executed one time more than the loop body. We assume that comments are
not executable statements, and so they take no time.

Running time of the algorithm is:

The running time of the algorithm is the sum of running times for each statement
executed; a statement that takes ci steps to execute and is executed n times will contribute ci n
to the total running time. To compute T(n), the running time of INSERTION-SORT, we sum the
products of the cost and times columns, obtaining

T(n) = C1n + C2(n-1) + 0(n-1) + C4(n-1) + C5 ∑ + C 6∑ ) + C7∑ ) + C8(n-1)

Compiled by: Prashant Agrawal, Associate Professor


KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2015 Certified & ‘A’ Grade accredited Institution by NAAC)
Data Structure
RCA 303: Session 2020-21

Best case: It occurs when Array is sorted.

All values are 1.


T(n) = C1n + C2(n-1) + C4(n-1) + C5 (n-1) + C8(n-1)
= (C1 + C2 + C4 + C5 + C8) n - ( C2 + C4 + C5 + C8)
Which is of the form of . It is a linear function of n. So linear growth.

Worst case: It occurs when Array is reverse sorted,

We must compare each element A[ j ] with each element in the entire sorted subarray A[1 . . j − 1], and
so for j = 2, 3, . . . , n. Noting that

So updated value of T(n) is

Which is of the form an2+bn+c. A Quadratic function. So in worst case insertion set grows in n2.

Compiled by: Prashant Agrawal, Associate Professor


KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2015 Certified & ‘A’ Grade accredited Institution by NAAC)
Data Structure
RCA 303: Session 2020-21
Why we concentrate on worst-case running time?

The worst-case running time gives a guaranteed upper bound on the running time for
any input.
For some algorithms, the worst case occurs often. For example, when searching, the worst case
often occurs when the item being searched for is not present, and searches for absent items
may be frequent.
Why not analyze the average case? Because it’s often about as bad as the worst case.

Order of growth:

It is described by the highest degree term of the formula for running time. (Drop lower-order terms.
Ignore the constant coefficient in the leading term.)

Example:

We found out that for insertion sort the worst-case running time is of the form an2+bn+c. Drop lower-
order terms. What remains is an2.Ignore constant coefficient. It results in n2.But we cannot say that the
worst-case running time T(n) equals n2 .Rather It grows like n2 . But it doesn’t equal n2.We say that the
running time is Θ (n2) to capture the notion that the order of growth is n2.

We usually consider one algorithm to be more efficient than another if its worst-case running time has a
smaller order of growth.

Compiled by: Prashant Agrawal, Associate Professor

You might also like