Class3-4 ADT Array Analysis Sorting InsertionSort Complexity 08-10jan2023
Class3-4 ADT Array Analysis Sorting InsertionSort Complexity 08-10jan2023
Algorithms
• Text Books:
• T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein,
Introduction to Algorithms, MIT Press, 3/e, 2009.
• Y. Langsam, M. J. Augenstein, and A. M. Tenenbaum, Data
Structures Using C and C++, Prentice Hall, 2/e, 2000.
• S. Sahni, Data Structures, Algorithms, and Applications in C++,
Silicon Press, 2/e, 2005.
• Michael T. Goodrich, Roberto Tamassia, and David M., Data
Structures and Algorithms in C++, John Wiley & Sons, Inc., 2/e,
2004
Array Data Structure
2
Array Data Structure
• Array is a composite or structured data type
• One-dimensional array:
– Finite ordered set of homogeneous elements
– Finite:
• There is a specific number of elements in the array
– Ordered:
• Elements are arranged so that zeroth, first, second, third and
so forth
– Homogeneous:
• All elements of array must be of same type
• Example:
– Array of 5 natural numbers: A = [2, 34, 54, 21, 5]
– Array of 9 characters: A = [‘c’, ‘h’, ‘a’, ‘r’, ‘a’, ‘c’, ‘t’, ‘r’, ‘s’]
3
Algorithms
• Algorithm is a tool for solving well-specified
computational problems
• An algorithm is any well-defined computational
procedure that
– takes some value, or set of values as input
– produces some value, or set of values as output
• Algorithm is a sequence of computational steps that
transform the input into the output
Specification of
output
Specification of Algorithm as
input
function of
input
4
Sorting Problem
Sequence of Sequence of
numbers Sort numbers
a1, a2,…, an a’1, a’2,…, a’n
5
Insertion Sort
Insertion Sort
INPUT : A[1..n], array of integers
OUTPUT: Rearrangement of A such that A[1]≤A[2] ≤… ≤A[n]
INSERTION-SORT (A)
1. for j = 2 to n
2. 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. A[i+1] = A[i]
7. i=i-1
8. A[i + 1] = key
7
Good Algorithm
• For the same problem, there can be infinitely many
algorithms
• Good algorithm means efficient algorithm
• Efficient algorithm:
– The algorithm with smaller running time (time
complexity) and takes less memory (space complexity)
• These measures of efficiency are seen as a function of
input size
• The time taken by the program will increases
with the increase in the input size
• Measuring running time:
– Experimental study
• Correctness of program only on the sample inputs
49
General Methodology for Analysing
Running Time
• Using of high-level description of the algorithm, instead of
testing one of its implementations
• This helps in evaluating the efficiency of any algorithm (as
a function of input size) independent of the hardware and
software requirement
• This analysis helps algorithm to scale automatically with
varying input size
• High level description specify the key aspect of algorithm
• High level description is given in the form of pseudo-code
• Identify the primitive operations in the pseudo-code
– Data movement statements, control statements, arithmetic
and logical operation statements
• Count the number primitive operations executed by the
algorithm
51
Analysis of Insertion Sort Algorithm
n
n 1
n 1
n
t
j 2
j
(t
j 2
j 1)
n
(t
j 2
j 1)
n 1
52
Analysis of Insertion Sort Algorithm
• Total Time T(n):
n n n
T (n) c1n c2 (n 1) c3 (n 1) c4 t j c5 (t j 1) c6 (t j 1) c7 (n 1)
j 2 j 2 j 2
n n
T (n) (c1 c2 c3 c7 )n c4 t j (c5 c6 ) (t j 1) (c2 c3 c7 )
j 2 j 2
53
Analysis of Insertion Sort Algorithm
n n
T (n) (c1 c2 c3 c7 )n c4 t j (c5 c6 ) (t j 1) (c2 c3 c7 )
j 2 j 2
• Best case: t j 1 Linear time
– The input array is already sorted
54
Analysis of Insertion Sort Algorithm
n n
T (n) (c1 c2 c3 c7 )n c4 t j (c5 c6 ) (t j 1) (c2 c3 c7 )
j 2 j 2
• Worst case: t j j Quadratic time
– The input array is already sorted in reverse order
c4 c5 c6 c4 c5 c6 2
T (n) c1 c2 c3 c7 n n (c2 c3 c4 c5 c6 c7 )
2 2
c 3 c c c c c c
T (n) c1 c2 c3 4 c7 c5 c6 n 4 5 6 n 2 c2 c3 4 5 6 c7
4 4 4 2
j
• Average case: t j Quadratic time
2
c 3 c c c c c c
T (n) c1 c2 c3 4 c7 c5 c6 n 4 5 6 n 2 c2 c3 4 5 6 c7
4 4 4 2
58