SlideShare a Scribd company logo
Complexity of an algorithm
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 1
• Sometimes, there are more than one way to solve a problem.
• We need to learn how to compare the performance of different
algorithms and choose the best one to solve a particular problem.
• While analyzing an algorithm, we mostly consider time complexity and
space complexity.
• Time complexity of an algorithm quantifies the amount of time taken by
an algorithm to run as a function of the length of the input.
• Space complexity of an algorithm quantifies the amount of space or
memory taken by an algorithm to run as a function of the length of the
input.
• Time & space complexity depends on lots of things like hardware,
operating system, processors, etc.
• However, we don't consider any of these factors while analyzing the
algorithm. We will only consider the execution time of an algorithm.
Unit 1 : Calculating Time Complexity
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 2
Q: What are the number of elements, If first element is at 1 and last element
is at 10 ?
Q: What are the no. of elements, if first element is at 527 and last element is
at 9346 ?
Length of array : UB – LB + 1
Unit 1 : Time space trade off
• Calculate Time Complexity of Algorithm
• Time Complexity is most commonly estimated by counting the number
of elementary functions performed by the algorithm.
• Since the algorithm's performance may vary with different types of
input data,
• Hence for an algorithm we usually use the worst-case Time complexity
of an algorithm because that is the maximum time taken for any input
size.
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 3
Unit 1 : Time space trade off
• Execution Time Cases
•There are three cases which are usually used to compare various data
structure's execution time in a relative manner.
 Worst Case − This is the scenario where a particular data structure
operation takes maximum time it can take. If an operation's worst case
time is ƒ(n) then this operation will not take more than ƒ(n) time
where ƒ(n) represents function of n.
 Average Case − This is the scenario depicting the average execution
time of an operation of a data structure. If an operation takes ƒ(n) time
in execution, then m operations will take mƒ(n) time.
 Best Case − This is the scenario depicting the least possible execution
time of an operation of a data structure. If an operation takes ƒ(n) time
in execution, then the actual operation may take time as the random
number which would be maximum as ƒ(n).
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 4
Case of : Insertion sort & related questions
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 5
• Q1: What are the number of comparison required to insert 40? // fig : 1
• Hint : j = 4 , i = 3
• Answer : 2
• Q2: How many times the while loop execute?
• Answer : 2
• // for i= 3 condition is true
• // for i=2 condition is false
• Q3 : What are the number of comparison required to insert 25 ? // fig :2
• Hint : j = 5, i = 4
• Answer : 4
• Q4: How many times the while loop execute?
• Answer: 4
• //for i=4,3,2 condition is true and for i= 1 condition is false
• Q5 : What are the number of comparison required to insert 10 ? //fig : 2
• Hint : j = 5, i=4
• Answer : 4
• Q6: How many times the while loop execute?
• Answer : 5 // for i = 4,3,2,1 condition is true & for i=0 condition is false
20 30 50
1 2 3
20 30 40 50
1 2 3 4
Algorithm IS (a,n)
{
for ( j=2 to n)
{ // j -> the position of element
which we want to insert in
sorted
// i -> the no. of elements
already in sorted array
// i -> max no of comparisons
Key = a[j] ;
i = j – 1;
while ( i > 0 && key < a[i])
{
Shift a[i] to +1
position &
decrement i;
}
Place key at its correct
position
}
}
Complexity : Insertion sort
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 6
• Algorithm IS (a,n)
• { Line 1. // this algo will sort the
elements of array a[], having n elements
• Line 2. for ( j=2 to n)
• {
• Line 3. Key = a[j] ;
• Line 4. i = j – 1;
• Line 5. while ( i > 0 && key <a[i])
• {
• Line 6. a[i+1] = a[i];
• Line 7. i = i -1 ;
• }
• Line 8. a[i+1] = key;
• }
• }
1. Comment 0
Line
No
How to find complexity Time taken
2 No of time the for loop execute (N-2+1) + 1=
n-1+1=n
3. Loop -1 N-1
4. Loop -1 N-1
5. While loop depends on comparison of
key, key is dependent on J, i.e a2, a3,
a4, a5 also loop depend on I, ie no of
sorted elements, so loop vary from 2 to
n . We assume that the while loop
execute times , where j is from 2 to 5
ie.
T2 + T3 + T4 +T5
6. While Loop -1
7. While Loop -1
8. For loop -1 N-1
Total complexity = sum of all time
T(n) = n + (n-1) + (n-1) + + + + n-1 --- eqn: 1
Analysis of Insertion Sort
Elements are in increasing order
• Case 1 : When the elements are in increasing order
• // tj => is the number of times the while loop execute
• j =2 , key 30, i=1
• While(1>0 & key < a1) // condition false
• => t2 = 1
• J=3, key = 40, i= 2
• While(2>0 & key< a2) // condition false
• => t3 = 1
• J=4, key = 50, i=3
• While (3>0 & key <a3) // condition false
• => t4 = 1
• J=5, key =60, I = 4
• While(4>0 & key <a4) // condition false
• => t5 = 1
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 7
20 30 40 50 60
1 2 3 4 5
• Algorithm IS (a,n)
• { // this algo will sort the elements
of array a[], having n elements
• for ( j=2 to n)
• {
• Key = a[j] ;
• i = j – 1;
• while ( i > 0 && key < a[i])
• {
• a[i+1] = a[i];
• i = i -1 ;
• }
• a[i+1] = key;
• }
• }
Analysis of Insertion Sort
Elements are in increasing order
• = 1 + 1 + 1 + 1 …………(n-2+1)
• = 1 + 1 + …….(n-1) = n-1
• 0
• // (n-1) – (n-1)= 0
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 8
Total complexity = sum of all time
T(n) = n + (n-1) + (n-1) + + + + n-1 --- eqn: 1
T(n) = n + (n-1) + (n-1) + (n-1) + 0 + 0 + (n-1)
T(n) = An + B where A and B are constants
T(n) = Ɵ (n) // theta(n)
Complexity when elements are in increasing order
Analysis of Insertion Sort
Elements are in decreasing order
• Case 2 : When the elements are in decreasing order
• // tj => is the number of times the while loop execute
• j =2 , key 50, i=1
• While(1>0 & key < a1) // condition true
• Shift 60 to second position, decrement i
• While (0>0 // condition false
• But again while loop execute,
• hence we can say that for j= 2 while loop execute twice
• J=3, key = 40, i= 2
• While(2>0 & key< a2) // condition true
• Shift 60 to 3rd
position and decrement i
• While ( 1>0 & key <a1) //condition true
• Shift 50 to 2nd
position, decrement i
• While( 0 > 0 // condition false
• Hence for j= 3 while loop executes thrice
Similarly for j=4 while loop executes 4 times
And so on
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 9
60 50 40 30 20
1 2 3 4 5
• Algorithm IS (a,n)
• { // this algo will sort the elements
of array a[], having n elements
• for ( j=2 to n)
• {
• Key = a[j] ;
• i = j – 1;
• while ( i > 0 && key < a[i])
• {
• a[i+1] = a[i];
• i = i -1 ;
• }
• a[i+1] = key;
• }
• }
50 60
1 2
40 50 60
1 2 3
Analysis of Insertion Sort
Elements are in decreasing order
• = 2 +3 + 4 + ………… + n
• = { n(n+1)/2 } - 1 // sum of n natural numbers is n(n+1)/2
• [{ n(n+1)/2 } - 1 ] – (n-1) = n(n-1)/2
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 10
Total complexity = sum of all time
T(n) = n + (n-1) + (n-1) + + + + n-1 --- eqn: 1
T(n) = n + (n-1) + (n-1) + [{n(n+1)/2} – 1] + n(n-1)/2 + n(n-1)/2 + (n-1)
T(n) = An2
+ Bn +C where A,B and C are constants
T(n) = O (n2
) // Big-oh(n2
)
Complexity when elements are in decreasing order
Complexity of Insertion sort
• T(n) = O (n2
) // Big-oh(n2
)
•Complexity when elements are in decreasing order
CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 11
Total complexity
T(n) = n + (n-1) + (n-1) + + + + n-1
T(n) = O (n2
) // Big-oh(n2
)
Complexity when elements are in decreasing order
Worst Case
T(n) = Ɵ (n) // theta(n)
Complexity when elements are in increasing order
Best Case
General Equation for Insertion Sort

More Related Content

PDF
Lecture 3 insertion sort and complexity analysis
PPTX
Algorithms - "Chapter 2 getting started"
PDF
Chapter One.pdf
PPSX
Lecture 2 data structures & algorithms - sorting techniques
PPTX
Analysis of Algorithms (CSE II/III year)
PPT
Lecture 1 and 2 of Data Structures & Algorithms
PPT
1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt
Lecture 3 insertion sort and complexity analysis
Algorithms - "Chapter 2 getting started"
Chapter One.pdf
Lecture 2 data structures & algorithms - sorting techniques
Analysis of Algorithms (CSE II/III year)
Lecture 1 and 2 of Data Structures & Algorithms
1 Analysis of algorithmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm.ppt

Similar to Lecture 2 DataStrucure - complexity , IS.pptx (20)

PDF
Data Structure & Algorithms - Mathematical
PPT
Chapter 1 & 2 - Introduction dhjgsdkjfsaf.ppt
PPTX
Data Structures and Algorithms for placements
PPTX
Sorting pnk
PPT
Data Structures 6
PPTX
Introduction to Algorithms
PPTX
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
PPT
03_sorting123456789454545454545444543.ppt
PPT
03_sorting and it's types with example .ppt
PPT
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
PPT
Design and Analysis of Algorithm Fundamental
PDF
Algorithm analysis
PPT
Insersion & Bubble Sort in Algoritm
PPTX
algorithmanalysis and effciency.pptx
PDF
Data Structure: Algorithm and analysis
PPTX
19. algorithms and-complexity
PPTX
Time and Space Complexity Analysis.pptx
PDF
Quick sort,bubble sort,heap sort and merge sort
PPTX
Lecture 3 complexity
PPTX
Intro to super. advance algorithm..pptx
Data Structure & Algorithms - Mathematical
Chapter 1 & 2 - Introduction dhjgsdkjfsaf.ppt
Data Structures and Algorithms for placements
Sorting pnk
Data Structures 6
Introduction to Algorithms
Ch 2Algo Analysis.pptxCh 2Algo Analysis.pptx
03_sorting123456789454545454545444543.ppt
03_sorting and it's types with example .ppt
quicksortnmsd cmz ,z m,zmm,mbfjjjjhjhfjsg
Design and Analysis of Algorithm Fundamental
Algorithm analysis
Insersion & Bubble Sort in Algoritm
algorithmanalysis and effciency.pptx
Data Structure: Algorithm and analysis
19. algorithms and-complexity
Time and Space Complexity Analysis.pptx
Quick sort,bubble sort,heap sort and merge sort
Lecture 3 complexity
Intro to super. advance algorithm..pptx
Ad

More from GauravKumar295392 (7)

PDF
Object Oriented Programming using C++ PCIT102.pdf
PDF
chap3expression-1603gggh13040831 (1).pdf
PDF
062636636366363773737373733+73737733+7.pdf
PPT
Ffghhhhfghhfffhjdsdhjkgffjjjkfdghhftgdhhhggg didi ucch JFK bcom
PPTX
TCS 204-SM0172637373838388383+8474747478484(4.pptx
PDF
Annotations.pdf
PDF
Dbms Model.pdf
Object Oriented Programming using C++ PCIT102.pdf
chap3expression-1603gggh13040831 (1).pdf
062636636366363773737373733+73737733+7.pdf
Ffghhhhfghhfffhjdsdhjkgffjjjkfdghhftgdhhhggg didi ucch JFK bcom
TCS 204-SM0172637373838388383+8474747478484(4.pptx
Annotations.pdf
Dbms Model.pdf
Ad

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
master seminar digital applications in india
PDF
Pre independence Education in Inndia.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
01-Introduction-to-Information-Management.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
RMMM.pdf make it easy to upload and study
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PDF
Sports Quiz easy sports quiz sports quiz
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
VCE English Exam - Section C Student Revision Booklet
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
master seminar digital applications in india
Pre independence Education in Inndia.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial diseases, their pathogenesis and prophylaxis
01-Introduction-to-Information-Management.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
RMMM.pdf make it easy to upload and study
O7-L3 Supply Chain Operations - ICLT Program
Complications of Minimal Access Surgery at WLH
Sports Quiz easy sports quiz sports quiz
2.FourierTransform-ShortQuestionswithAnswers.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
GDM (1) (1).pptx small presentation for students
PPH.pptx obstetrics and gynecology in nursing
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Lecture 2 DataStrucure - complexity , IS.pptx

  • 1. Complexity of an algorithm CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 1 • Sometimes, there are more than one way to solve a problem. • We need to learn how to compare the performance of different algorithms and choose the best one to solve a particular problem. • While analyzing an algorithm, we mostly consider time complexity and space complexity. • Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the length of the input. • Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of the length of the input. • Time & space complexity depends on lots of things like hardware, operating system, processors, etc. • However, we don't consider any of these factors while analyzing the algorithm. We will only consider the execution time of an algorithm.
  • 2. Unit 1 : Calculating Time Complexity CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 2 Q: What are the number of elements, If first element is at 1 and last element is at 10 ? Q: What are the no. of elements, if first element is at 527 and last element is at 9346 ? Length of array : UB – LB + 1
  • 3. Unit 1 : Time space trade off • Calculate Time Complexity of Algorithm • Time Complexity is most commonly estimated by counting the number of elementary functions performed by the algorithm. • Since the algorithm's performance may vary with different types of input data, • Hence for an algorithm we usually use the worst-case Time complexity of an algorithm because that is the maximum time taken for any input size. CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 3
  • 4. Unit 1 : Time space trade off • Execution Time Cases •There are three cases which are usually used to compare various data structure's execution time in a relative manner.  Worst Case − This is the scenario where a particular data structure operation takes maximum time it can take. If an operation's worst case time is ƒ(n) then this operation will not take more than ƒ(n) time where ƒ(n) represents function of n.  Average Case − This is the scenario depicting the average execution time of an operation of a data structure. If an operation takes ƒ(n) time in execution, then m operations will take mƒ(n) time.  Best Case − This is the scenario depicting the least possible execution time of an operation of a data structure. If an operation takes ƒ(n) time in execution, then the actual operation may take time as the random number which would be maximum as ƒ(n). CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 4
  • 5. Case of : Insertion sort & related questions CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 5 • Q1: What are the number of comparison required to insert 40? // fig : 1 • Hint : j = 4 , i = 3 • Answer : 2 • Q2: How many times the while loop execute? • Answer : 2 • // for i= 3 condition is true • // for i=2 condition is false • Q3 : What are the number of comparison required to insert 25 ? // fig :2 • Hint : j = 5, i = 4 • Answer : 4 • Q4: How many times the while loop execute? • Answer: 4 • //for i=4,3,2 condition is true and for i= 1 condition is false • Q5 : What are the number of comparison required to insert 10 ? //fig : 2 • Hint : j = 5, i=4 • Answer : 4 • Q6: How many times the while loop execute? • Answer : 5 // for i = 4,3,2,1 condition is true & for i=0 condition is false 20 30 50 1 2 3 20 30 40 50 1 2 3 4 Algorithm IS (a,n) { for ( j=2 to n) { // j -> the position of element which we want to insert in sorted // i -> the no. of elements already in sorted array // i -> max no of comparisons Key = a[j] ; i = j – 1; while ( i > 0 && key < a[i]) { Shift a[i] to +1 position & decrement i; } Place key at its correct position } }
  • 6. Complexity : Insertion sort CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 6 • Algorithm IS (a,n) • { Line 1. // this algo will sort the elements of array a[], having n elements • Line 2. for ( j=2 to n) • { • Line 3. Key = a[j] ; • Line 4. i = j – 1; • Line 5. while ( i > 0 && key <a[i]) • { • Line 6. a[i+1] = a[i]; • Line 7. i = i -1 ; • } • Line 8. a[i+1] = key; • } • } 1. Comment 0 Line No How to find complexity Time taken 2 No of time the for loop execute (N-2+1) + 1= n-1+1=n 3. Loop -1 N-1 4. Loop -1 N-1 5. While loop depends on comparison of key, key is dependent on J, i.e a2, a3, a4, a5 also loop depend on I, ie no of sorted elements, so loop vary from 2 to n . We assume that the while loop execute times , where j is from 2 to 5 ie. T2 + T3 + T4 +T5 6. While Loop -1 7. While Loop -1 8. For loop -1 N-1 Total complexity = sum of all time T(n) = n + (n-1) + (n-1) + + + + n-1 --- eqn: 1
  • 7. Analysis of Insertion Sort Elements are in increasing order • Case 1 : When the elements are in increasing order • // tj => is the number of times the while loop execute • j =2 , key 30, i=1 • While(1>0 & key < a1) // condition false • => t2 = 1 • J=3, key = 40, i= 2 • While(2>0 & key< a2) // condition false • => t3 = 1 • J=4, key = 50, i=3 • While (3>0 & key <a3) // condition false • => t4 = 1 • J=5, key =60, I = 4 • While(4>0 & key <a4) // condition false • => t5 = 1 CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 7 20 30 40 50 60 1 2 3 4 5 • Algorithm IS (a,n) • { // this algo will sort the elements of array a[], having n elements • for ( j=2 to n) • { • Key = a[j] ; • i = j – 1; • while ( i > 0 && key < a[i]) • { • a[i+1] = a[i]; • i = i -1 ; • } • a[i+1] = key; • } • }
  • 8. Analysis of Insertion Sort Elements are in increasing order • = 1 + 1 + 1 + 1 …………(n-2+1) • = 1 + 1 + …….(n-1) = n-1 • 0 • // (n-1) – (n-1)= 0 CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 8 Total complexity = sum of all time T(n) = n + (n-1) + (n-1) + + + + n-1 --- eqn: 1 T(n) = n + (n-1) + (n-1) + (n-1) + 0 + 0 + (n-1) T(n) = An + B where A and B are constants T(n) = Ɵ (n) // theta(n) Complexity when elements are in increasing order
  • 9. Analysis of Insertion Sort Elements are in decreasing order • Case 2 : When the elements are in decreasing order • // tj => is the number of times the while loop execute • j =2 , key 50, i=1 • While(1>0 & key < a1) // condition true • Shift 60 to second position, decrement i • While (0>0 // condition false • But again while loop execute, • hence we can say that for j= 2 while loop execute twice • J=3, key = 40, i= 2 • While(2>0 & key< a2) // condition true • Shift 60 to 3rd position and decrement i • While ( 1>0 & key <a1) //condition true • Shift 50 to 2nd position, decrement i • While( 0 > 0 // condition false • Hence for j= 3 while loop executes thrice Similarly for j=4 while loop executes 4 times And so on CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 9 60 50 40 30 20 1 2 3 4 5 • Algorithm IS (a,n) • { // this algo will sort the elements of array a[], having n elements • for ( j=2 to n) • { • Key = a[j] ; • i = j – 1; • while ( i > 0 && key < a[i]) • { • a[i+1] = a[i]; • i = i -1 ; • } • a[i+1] = key; • } • } 50 60 1 2 40 50 60 1 2 3
  • 10. Analysis of Insertion Sort Elements are in decreasing order • = 2 +3 + 4 + ………… + n • = { n(n+1)/2 } - 1 // sum of n natural numbers is n(n+1)/2 • [{ n(n+1)/2 } - 1 ] – (n-1) = n(n-1)/2 CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 10 Total complexity = sum of all time T(n) = n + (n-1) + (n-1) + + + + n-1 --- eqn: 1 T(n) = n + (n-1) + (n-1) + [{n(n+1)/2} – 1] + n(n-1)/2 + n(n-1)/2 + (n-1) T(n) = An2 + Bn +C where A,B and C are constants T(n) = O (n2 ) // Big-oh(n2 ) Complexity when elements are in decreasing order
  • 11. Complexity of Insertion sort • T(n) = O (n2 ) // Big-oh(n2 ) •Complexity when elements are in decreasing order CS201, DS, B.Tech CSE/IT/EE 3rd Semester, 2nd Year 11 Total complexity T(n) = n + (n-1) + (n-1) + + + + n-1 T(n) = O (n2 ) // Big-oh(n2 ) Complexity when elements are in decreasing order Worst Case T(n) = Ɵ (n) // theta(n) Complexity when elements are in increasing order Best Case General Equation for Insertion Sort