Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Algorithm Analysis: Heap Sort and Related Algorithms, Study notes of Algorithms and Programming

An analysis of heap sort algorithm and its implementation, including the build-max-heap and max-heapify procedures. Additionally, it covers related algorithms such as merge sort, binary search algorithm (recursive), and longest common subsequence using dynamic programming. The document also includes examples and explanations of each algorithm.

What you will learn

  • What is the difference between heap sort and merge sort?
  • How does the binary search algorithm work?
  • What is the time complexity of the longest common subsequence algorithm?
  • What is the time complexity of heap sort algorithm?
  • How does the merge sort algorithm work?

Typology: Study notes

2022/2023

Uploaded on 12/01/2022

shiv-patel-6
shiv-patel-6 🇮🇳

1 / 105

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Unit-1 Basics of Algorithms and
Mathematics
1
Prof. Rupesh G. Vaishnav, CE Department
| 2150703 Analysis and Design of Algorithm s
(1) Algorithm and Properties of Algorithm
Algorithm
An algorithm is any well-defined computational procedure that takes some values or set of values as
input and produces some values or set of values as output.
An algorithm is a sequence of computational steps that transform the input into the output.
An algorithm is a set of rules for carrying out calculation either by hand or on a machine.
An algorithm is an abstraction of a program to be executed on a physical machine (model of
Computation).
Properties of Algorithm
All the algorithms should satisfy following five properties,
1) Input: There is zero or more quantities supplied as input to the algorithm.
2) Output: By using inputs which are externally supplied the algorithm produces at least one quantity
as output.
3) Definiteness: The instructions used in the algorithm s pecify one or more operations. These
operations must be clear and unambiguous. This implies that each of these operations must be
definite; clearly specifying what is to be done.
4) Finiteness: Algorithm must terminate after some finite number of steps for all cases.
5) Effectiveness: The instructions which are used to accomplish the task must be basic i.e. the human
being can trace the instructions by using paper and pencil in every way.
(2) Mathematics for Algorithmic Set
Set
Unordered collection of distinct elements. Can be represented either by property or by value.
Set Cardinality
The number of elements in a set is called cardinality or size of the set, denoted |S| or sometimes n(S).
The two sets have same cardinality if their elements can be put into a one-to-one correspondence.
It is easy to see that the cardinality of an empty set is zero i.e., | ø |.
Multiset
If we do want to take the number of occurrences of members into account, we call the group a
multiset.
For example, {7} and {7, 7} are identical as set but {7} and {7, 7} are different as multiset.
Infinite Set
A set contains infinite elements. For example, set of negative integers, set of integers, etc
Empty Set
Set contain no member, denoted as { } or ø.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Algorithm Analysis: Heap Sort and Related Algorithms and more Study notes Algorithms and Programming in PDF only on Docsity!

Unit- 1 – Basics of Algorithms and

Mathematics

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

(1) Algorithm and Properties of Algorithm

Algorithm

 An algorithm is any well-defined computational procedure that takes some values or set of values as

input and produces some values or set of values as output.

 An algorithm is a sequence of computational steps that transform the input into the output.

 An algorithm is a set of rules for carrying out calculation either by hand or on a machine.

 An algorithm is an abstraction of a program to be executed on a physical machine (model of

Computation).

Properties of Algorithm

 All the algorithms should satisfy following five properties,

  1. Input: There is zero or more quantities supplied as input to the algorithm.

  2. Output: By using inputs which are externally supplied the algorithm produces at least one quantity

as output.

  1. Definiteness: The instructions used in the algorithm specify one or more operations. These

operations must be clear and unambiguous. This implies that each of these operations must be

definite; clearly specifying what is to be done.

  1. Finiteness: Algorithm must terminate after some finite number of steps for all cases.

  2. Effectiveness: The instructions which are used to accomplish the task must be basic i.e. the human

being can trace the instructions by using paper and pencil in every way.

(2) Mathematics for Algorithmic Set

Set

Unordered collection of distinct elements. Can be represented either by property or by value.

Set Cardinality

 The number of elements in a set is called cardinality or size of the set, denoted |S| or sometimes n(S).

 The two sets have same cardinality if their elements can be put into a one-to-one correspondence.

It is easy to see that the cardinality of an empty set is zero i.e., | ø |.

Multiset

 If we do want to take the number of occurrences of members into account, we call the group a

multiset.

 For example, {7} and {7, 7} are identical as set but {7} and {7, 7} are different as multiset.

Infinite Set

A set contains infinite elements. For example, set of negative integers, set of integers, etc …

Empty Set

Set contain no member, denoted as { } or ø.

Unit- 1 – Basics of Algorithms and

Mathematics

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

Subset

For two sets A and B, we say that A is a subset of B, written A B, if every member of set A is also a

member of set B. Formally, A B if x ϵ A implies x ϵ B

Proper Subset

Set A is a proper subset of B, written A B, if A is a subset of B and not equal to B. That is, a set A is

proper subset of B if A B but A B.

Equal Sets

The sets A and B are equal, written A = B, if each is a subset of the other.

Let A and B be sets. A = B if A B and B A.

Power Set

 Let A be the set. The power of A, written P(A) or 2

A

, is the set of all subsets of A. That is, P (A) = {B: B

A}.

 For example, consider A= {0, 1}.

The power set of A is {{}, {0}, {1}, {0, 1}}.

Union of sets

 The union of A and B, written A B, is the set we get by combining all elements in A and B into a

single set.

 That is, A B = {x: x ϵ A or x ϵ B}.

Disjoint sets

 Let A and B be sets. A and B are disjoint if A ∩ B =.

Intersection sets

 The intersection of set A and B, written A ∩ B, is the set of elements that are both in A and in B. That

is, A ∩ B = { x : x ϵ A and x ϵ B}.

Difference of Sets

 Let A and B be sets. The difference of A and B is A - B = {x : x ϵ A and x B}.

 For example, let A = {1, 2, 3} and B = {2, 4, 6, 8}. The set difference A - B = {1, 3} while B-A = {4, 6, 8}.

Complement of a set

 All set under consideration are subset of some large set U called universal set.

 Given a universal set U, the complement of A, written A', is the set of all elements under

consideration that are not in A.

 Formally, let A be a subset of universal set U.

 The complement of A in U is A' = A - U OR A' = {x: x ϵ U and x A}.

Unit- 1 – Basics of Algorithms and

Mathematics

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

 So that by using this concept we can say all functions are considered as relation also but not vice

versa.

Properties of the Relation

 Different relations can observe some special properties namely reflexive, symmetric , transitive and

Anti symmetric.

Reflexive:

 When for all values is true then relation R is said to be reflexive.

 E.g. the equality (=) relation is reflexive.

Symmetric:

 When for all values of x and y, is true. Then we can say that relation R is symmetric.

Equality (=) relation is also symmetric.

Transitive:

 When for all values of x, y and z, x R y and y R z then we can say that x R z, which is known as transitive

property of the relation.

 E.g. the relation grater then > is transitive relation.

 If x>y and y>z then we can say that x>z i.e. x is greater than y and y is greater than z then x is also

greater than z.

Anti-symmetric:

 When for all values of x and y if x R y and y R x implies x=y then relation R is Anti - symmetric.

 Anti-symmetric property and symmetric properties are lookalike same but they are different.

 E.g. consider the relation greater than or equal to if x y and y x then we can say that y = x.

 A relation is Anti-symmetric if and only if x X and (x, x) R.

Equivalence Relation:

 Equivalence Relation plays an important role in discrete mathematics.

 Equivalent relation declares or shows some kind of equality or say equivalence.

 The relation is equivalent only when it satisfies all following property i.e. relation must be reflexive,

symmetric and transitive then it is called Equivalence Relation.

 E.g. Equality ‘=’ relation is equivalence relation because equality proves above condition i.e. it is

reflexive, symmetric and transitive.

o Reflexive: x=x is true for all values of x. so we can say that ’=’ is reflexive.

o Symmetric: x=y and y=x is true for all values of x and y then we can say that ‘=’ is symmetric.

o Transitive: if x=y and y=z is true for all values then we can say that x=z. thus’ =’ is transitive.

Unit- 1 – Basics of Algorithms and

Mathematics

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

(4) Quantifiers

 There are two basic quantifiers.

o Universal Quantification:

P (a) is the preposition, if P (a) is give expected result for all values of a in the universe of

discourse. The universal quantification of P (a) is denoted by, ( ). So is called as for all i.e. it

is the Universal Quantifier.

o Existential Quantification:

P (a) is the preposition, if there exits an element a in the universe of discourse such that P (a) is

giving expected result. Here the Existential Quantification of P (a) is represented by

( )

called as for some, i.e. it is Existential Quantifier.

(5) Vectors and Matrices

 A vector, u, means a list (or n-tuple) of numbers:

u = (u 1 , u 2 ,... , un)

 Where ui are called the components of u. If all the ui are zero i.e., ui = 0, then u is called the zero

vector.

 Given vectors u and v are equal i.e., u = v, if they have the same number of components and if

corresponding components are equal.

Addition of Two Vectors

 If two vectors, u and v, have the number of components, their sum, u + v, is the vector obtained by

adding corresponding components from u and v.

u + v = (u 1 , u 2 ,... , un) + (v 1 , v 2 ,... , vn)

= (u 1 + v 1 + u 2 + v 2 ,... , un + vn)

Multiplication of a vector by a Scalar

 The product of a scalar k and a vector u i.e., ku, is the vector obtained by multiplying each component

of u by k:

ku = k(u 1 , u 2 ,... , un)

= ku 1 , ku 2 ,... , kun

 It is not difficult to see k(u + v) = ku + kv where k is a scalar and u and v are vectors

Dot Product and Norm

 The dot product or inner product of vectors u = (u 1 , u 2 ,... , un) and v = (v 1 , v 2 ,... , vn) is denoted by

u.v and defined by

u.v = u 1 v 1 + u 2 v 2 +... + unvn

The norm or length of a vector, u, is denoted by ||u|| and defined by

Unit- 1 – Basics of Algorithms and

Mathematics

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

iv. A + (-A) = (-A) + A = 0

v. k(A + B) = kA + kB

vi. (k + I)A = kA + I A

vii. (k I)A = k(I A)

viii. I A = A

Matrix Multiplication

 Suppose A and B are two matrices such that the number of columns of A is equal to number of rows

of B. Say matrix A is an m×p matrix and matrix B is a p×n matrix. Then the product of A and B is the

m×n matrix whose ij-entry is obtained by multiplying the elements of the i

th

row of A by the

corresponding elements of the j

th

column of B and then adding them.

 It is important to note that if the number of columns of A is not equal to the number of rows of B,

then the product AB is not defined.

Properties of Matrix Multiplication

Let A, B, and C be matrices and let k be a scalar. Then

i. (AB)C = A(BC)

ii. A(B+C) = AB + AC

iii. (B+C)A = BA + CA

iv. k(AB) = (kB)B = A(kB)

Transpose

 The transpose of a matrix A is obtained by writing the row of A, in order, as columns and denoted by

AT. In other words, if A - (Aij), then B = (bij) is the transpose of A if bij - aji for all i and j.

 It is not hard to see that if A is an m×n matrix, then AT is an n×m matrix.

For example if

A = then AT =

Square Matrix

 If the number of rows and the number of columns of any matrix are same, we say matrix is a square

matrix, i.e., a square matrix has same number of rows and columns. A square matrix with n rows and

n columns is said to be order n and is called an n-square matrix.

 The main diagonal, or simply diagonal, of an n-square matrix A = (aij) consists of the elements a ( 11 ), a

( 22 ), a ( 33 )... a (mn).

Unit Matrix

 The n-square matrix with 1's along the main diagonal and 0's elsewhere is called the unit matrix and

usually denoted by I.

 The unit matrix plays the same role in matrix multiplication as the number 1 does in the usual

Unit- 1 – Basics of Algorithms and

Mathematics

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

multiplication of numbers.

A I = I A = A for any square matrix A.

(6) Linear Inequalities and Linear Equations.

Inequalities

The term inequality is applied to any statement involving one of the symbols <, >, ≤, ≥.

Examples of inequalities are:

i. x≥ 1

ii. x + y + 2z > 16

iii. p

2

  • q

2

iv. a

2

  • ab > 1

Fundamental Properties of Inequalities

  1. If a≤b and c is any real number, then a + c≤b + c.

For example, - 3 ≤-1 implies - 3+4≤-1 + 4.

  1. If a≤b and c is positive, then ac≤bc.

For example, 2≤3 implies 2(4) ≤3(4).

  1. If a≤b and c is negative, then ac≥bc.

For example, 3≤9 implies 3(-2) ≥9(-2).

  1. If a≤b and b≤c, then a≤ c.

For example, - 1/2≤2 and 2≤8/3 imply - 1/2≤8/3.

Solution of Inequality

 By solution of the one variable inequality 2x + 3≤ 7 we mean any number which substituted for x yields

a true statement.

 For example, 1 is a solution of 2x + 3≤7 since 2(1) + 3 = 5 and 5 is less than and equal to 7.

 By a solution of the two variable inequality x - y≤5 we mean any ordered pair of numbers which when

substituted for x and y, respectively, yields a true statement.

 For example, (2, 1) is a solution of x - y≤5 because 2-1 = 1 and 1≤5.

 By a solution of the three variable inequalities 2x - y + z≥3 we means an ordered triple of number

which when substituted for x, y and z respectively, yields a true statement.

 For example, (2, 0, 1) is a solution of 2x - y + z≤3.

 A solution of an inequality is said to satisfy the inequality. For example, (2, 1) is satisfy x - y≤5.

Linear Equations

One Unknown

 A linear equation in one unknown can always be stated into the standard form

ax = b

 Where x is an unknown and a and b are constants. If a is not equal to zero, this equation has a unique

Unit- 1 – Basics of Algorithms and

Mathematics

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

(7) Algorithm Analysis Measures and Design Techniques.

Algorithm Analysis Measures

 Measuring Space complexity

 Measuring Time complexity

 Input size

 Computing Best case, Average case, Worst case

 Computing order of growth of algorithm.

Algorithm Design Techniques

 Divide and Conquer

 Greedy Approach

 Dynamic Programming

 Branch and Bound

 Backtracking

Randomized Algorithm

(8) Explain why analysis of algorithms is important

 When we have a problem to solve, there may be several suitable algorithms available. We would

obviously like to choose the best.

 Analyzing an algorithm has come to mean predicting the resources that the algorithm requires.

 Generally, by analyzing several candidate algorithms for a problem, a most efficient one can be easily

identified.

 Analysis of algorithm is required to decide which of the several algorithms is preferable.

 There are two different approaches to analyze an algorithm.

  1. Empirical (posteriori) approach to choose an algorithm: Programming different competing

techniques and trying them on various instances with the help of computer.

  1. Theoretical (priori) approach to choose an algorithm: Determining mathematically the quantity of

resources needed by each algorithm as a function of the size of the instances considered. The

resources of most interest are computing time (time complexity) and storage space (space

complexity). The advantage is this approach does not depend on programmer, programming

language or computer being used.

Unit 2- Analysis of Algorithm

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

(1) The Efficient Algorithm

 Analysis of algorithm is required to measure the efficiency of algorithm.

 Only after determining the efficiency of various algorithms, you will be able to make a well informed

decision for selecting the best algorithm to solve a particular problem.

 We will compare algorithms based on their execution time. Efficiency of an algorithm means how

fast it runs.

 If we want to measure the amount of storage that an algorithm uses as a function of the size of the

instances, there is a natural unit available Bit.

 On the other hand, is we want to measure the efficiency of an algorithm in terms of time it takes to

arrive at result, there is no obvious choice.

 This problem is solved by the principle of invariance , which states that two different

implementations of the same algorithm will not differ in efficiency by more than some multiplicative

constant.

 Suppose that the time taken by an algorithm to solve an instance of size n is never more than cn

seconds, where c is some suitable constant.

 Practically size of instance means any integer that in some way measures the number of components

in an instance.

 Sorting problem: size is no. of items to be sorted.

 Graph: size is no. of nodes or edges or both involved.

 We say that the algorithm takes a time in the order of n i.e. it is a linear time algorithm.

 If an algorithm never takes more than cn

2

seconds to solve an instance of size n, we say it takes time

in the order of cn

2

i.e. quadratic time algorithm.

 Some algorithms behave as Polynomial : (

n

or n

k

) , Exponential : (c

n

or n!), Cubic : (n

3

or 5n

3

+n

2

Logarithmic: (log n or n log n)

(2) Worst Case, Best Case & Average Case Complexity.

Worst Case Analysis

 In the worst case analysis, we calculate upper bound on running time of an algorithm.

 We must know the case that causes maximum number of operations to be executed.

 e.g. For Linear Search, the worst case happens when the element to be searched (x in the above code) is not

present in the array.

 When x is not present, the search () functions compares it with all the elements of arr[] one by one.

Average Case Analysis

 In average case analysis, we take all possible inputs and calculate computing time for all of the inputs.

 Sum all the calculated values and divide the sum by total number of inputs. We must know (or predict)

distribution of cases.

 For the linear search problem, let us assume that all cases are uniformly distributed (including the case of x

not being present in array).

 So we sum all the cases and divide the sum by (n+1)

Unit 2- Analysis of Algorithm

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

3) Potential Method

 It is similar to the accounting method.

 Instead of representing prepaid work as credit it represents prepaid work as “Potential Energy”.

 Suppose initial data structure is D

0

for n operations D

0,

D

1,

D

2

… D

n

be the data structures let C

1,

C

2

C

n

denotes actual cost.

 φ is potential function that maps Data structure Di to a real number φ (Di)

(4) Asymptotic Notations.

Asymptotic notation is used to describe the running time of an algorithm.

It shows order of growth of function.

Θ-Notation (Same order)

 For a given function g(n), we denote by Θ( g ( n )) the set of functions

Θ( g ( n )) = { f(n) : there exist positive constants c

1

, c

2

and n

0

such that

0 ≤ c

1

g(n) ≤ f (n) ≤ c

2

g(n) for all n ≥ n

0

 Because Θ( g ( n )) is a set, we could write f(n) € Θ( g ( n )) to indicate that f(n) is a member of Θ( g ( n )).

 This notation bounds a function to within constant factors. We say f ( n ) = Θ( g ( n )) if there exist

positive constants n

0

, c

1

and c

2

such that to the right of n

0

the value of f ( n ) always lies between c

1

g ( n )

and c

2

g ( n ) inclusive.

 Figure a gives an intuitive picture of functions f(n) and g(n). For all values of n to the right of n

0

, the

value of f(n) lies at or above c

1

g(n) and at or below c

2

g(n). In other words, for all n ≥ n

0

, the value of

f(n) is equal to g(n) to within a constant factor.

 We say that g(n) is an asymptotically tight bound for f(n).

O-Notation (Upper Bound)

 For a given function g(n), we denote by Ο( g ( n )) the set of functions

Ο( g ( n )) = { f(n) : there exist positive constants c and n

0

such that

0 ≤ f (n) ≤ cg(n) for all n ≥ n

0

 We use Ο notation to give an upper bound on a function, to within a constant factor. For all values of

Unit 2- Analysis of Algorithm

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

n to the right of n 0

, the value of the function f(n) is on or below g(n).

 This notation gives an upper bound for a function to within a constant factor. We write f ( n ) = O ( g ( n ))

if there are positive constants n 0

and c such that to the right of n 0

, the value of f ( n ) always lies on or

below cg ( n ).

 We say that g(n) is an asymptotically upper bound for f(n).

Example:

Let f(n)=n

2

and g(n)=

n

n f(n)=n

2

g(n)=

n

1 1 2 f(n) < g(n) Here for n ≥ 4 we have

behavior f (n) ≤ g(n)

Where n 0

f(n) = g(n)

f(n) > g(n)

f(n) = g(n)

f(n) < g(n)

f(n) < g(n)

f(n) < g(n)

Ω-Notation (Lower Bound)

 For a given function g(n), we denote by Ω( g ( n )) the set of functions

Ω ( g ( n )) = { f(n) : there exist positive constants c and n 0

such that

0 ≤ cg(n) ≤ f (n)for all n ≥ n 0

Ω Notation provides an asymptotic lower bound. For all values of n to the right of n 0

, the value of

the function f(n) is on or above cg(n).

 This notation gives a lower bound for a function to within a constant factor. We write f ( n ) = Ω( g ( n )) if

there are positive constants n

0

and c such that to the right of n

0

, the value of f ( n ) always lies on or

above cg ( n ).

Unit 2- Analysis of Algorithm

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

Example: 3

for i=1 to n

for j=1 to n

b = a * c

end

end

C1: n+

C2: n+

n

C3: n

T(n) =C1+C2 +C

=n+1+n(n+1)+n(n) = 2n

2

+2n+

=O(n

2

(6) Insertion sort

 Insertion Sort works by inserting an element into its appropriate position during each iteration.

 Insertion sort works by comparing an element to all its previous elements until an appropriate

position is found.

 Whenever an appropriate position is found, the element is inserted there by shifting down remaining

elements.

Algorithm

Analysis

 The running time of an algorithm on a particular input is the number of primitive operations or

"steps" executed.

 A constant amount of time is required to execute each line of our pseudo code. One line may take a

different amount of time than another line, but we shall assume that each execution of the i

th

line

takes time c

i

, where c

i

is a constant.

 The running time of the algorithm is the sum of running times for each statement executed; a

statement that takes c i

steps to execute and is executed n times will contribute c i

n to the total

running time.

 Let the time complexity of selection sort is given as T(n), then

T(n) = C

1

n+ C

2

(n-1)+ C

3

(n-1)+ C

4

)+(C

5

+C

6

+ C

7

(n-1).

Procedure insert (T[1…. n ]) cost times

for i ← 2 to n do C1 n

x ← T[i] C2 n- 1

j ← i - 1 C3 n- 1

while j > 0 and x < T[j] do C4 ∑

T[j+1] ← T[j] C5 ∑

  • 1

j ← j - 1

end

C ∑ - 1

T[j + 1] ← x

end

C7 n- 1

Unit 2- Analysis of Algorithm

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

Where,

Best case:

Take j = 1

T(n) = C 1

n + C 2

(n-1) + C 3

(n-1) + C 4

(n-1) + C 5

(n-1)+ c 7

(n-1)

= (C

1

+C

2

+C

3

+C

4

+C

7

) n (C 2

+C

3

+C

4

+C

7

= an b

Thus, T(n) = Θ(n)

Worst case: Take j =n

T (n) = C

1

n+ C

2

(n-1)+ C

3

(n-1)+ C

4

)+(C

5

+C

6

+ C

7

(n-1).

= C

1

n+ C 2

n+ C 3

n+ C 4

+C

5

+ C

6

+ C

4

+ C

5

+ C

6

+ C

7

n-C 2

- C

3

- C

= n

2

(C

4

+ C

5

+ C

6

)+n(C

1

+C

2

+C

3

+C

7

+ C

4

+ C

5

+ C

6

)-1(C

2

+C

3

+C

7

= an

2

  • bn + c .

Thus, T(n) = Θ(n

2

Average case: Average case will be same as worst case T(n) = Θ(n

2

Time complexity of insertion sort

Best case Average case Worst case

O(n) O (n

2

) O (n

2

Unit 2- Analysis of Algorithm

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

(8) Bubble sort

 In the bubble sort, the consecutive elements of the table are compared and if the keys of the two

elements are not found in proper order, they are interchanged.

 It starts from the beginning of the table and continue till the end of the table. As a result of this the

element with the largest key will be pushed to the last element’s position.

 After this the second pass is made. The second pass is exactly like the first one except that this time

the elements except the last are considered. After the second pass, the next largest element will be

pushed down to the next to last position.

Algorithm

Analysis

T(n)=C1 (n+1) + C2 ∑ ( ) + C3 ∑ ( ) + C4 ∑ ( )

Best case:

Take i = 1

T(n) = C

1

n + C

1

+ C

2

n + C

3

n – C

3

+ C

4

n – C

4

= (C

1

+C

2

+C

3

+C

4

) n (C

2

,C

3

,C

4

,C

7

= an b

Thus, T(n) = Θ(n)

Worst Case:

= C 1

n+ C 1

  • C 2

n+ C 2

  • C 2

(

( )

) + C 3

n – C 3

(

( )

) +C 4

n – C 4

(

( )

)

= [–C

2

/n

2

– C

3

/n

2

– C

4

/n

2

] +[– C

2

/n–C 3

/n–C 4

/n]+C 1

+C

2

+C

3

+C

4

= an

2

+bn+c

= Θ (n

2

Average case: Average case will be same as worst case T(n) = Θ(n

2

Example:

Consider the following numbers are stored in an array:

Original Array: 32,51,27,85,66,23,13,

Pass 1 : 32,27,51,66,23,13,57,

Pass 2 : 27,33,51,23,13,57,66,

Pass 3 : 27,33,23,13,51,57,66,

Pass 4 : 27,23,13,33,51,57,66,

Procedure bubble (T[1…. n ]) cost times

for i ← 1 to n do C1 n+

for i ← 1 to n-i do C

∑ ( )

if T[i] > T[j] C

∑ ( )

T[i] ↔ T[j] C

∑ ( )

end

end

Unit 2- Analysis of Algorithm

Prof. Rupesh G. Vaishnav, CE Department

| 2150703 – Analysis and Design of Algorithms

Pass 5 : 23,13,27,33,51,57,66,

Pass 6 : 13,23,27,33,51,57,66,

(9) Selection sort

 Selection Sort works by repeatedly selecting elements.

 The algorithm finds the smallest element in the array first and exchanges it with the element in the first

position.

Then it finds the second smallest element and exchanges it with the element in the second position and continues

in this way until the entire array is sorted.

Algorithm

Procedure select ( T [1….n] ) Cost times

for i←1 to n- 1 do C1 n

minj ← i ; minx ← T[i] C2 n- 1

for j←i+1 to n do C

∑ ( )

if T[j] < minx then minj ← j C4 ∑

minx ← T[j] C5 ∑

T[minj] ← T[i] C6 n- 1

T[i] ← minx C7 n- 1

Analysis

T(n)=C

1

n+ C

2

(n-1)+ C

3

) C

4

)+C

5

)+C

6

(n-1)+ C

7

(n-1)

= C

1

n+ C

2

n+ C

6

n+ C

7

n+ C

3

+C

4

+ C

5

+ C

3

+ C

4

+ C

5

- C

2

- C

6

- C

7

=n(C 1

+ C

2

+ C

6

+C

7

+ C

3

+ C

4

+ C

5

) + n

2

(C

3

+ C

4

+ C

5

)-1(C

2

+ C

6

+C

7

= an

2

+bn+c

Time complexity of insertion sort

Best case Average case Worst case

O(n

2

) O (n

2

) O (n

2