SlideShare a Scribd company logo
1 of 20
Insertion Sort
for (int i = 1; i < a.length; i++)
{// insert a[i] into a[0:i-1]
int t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = t;
}
Complexity
 Space/Memory
 Time
– Count a particular operation
– Count number of steps
– Asymptotic complexity
Comparison Count
for (int i = 1; i < a.length; i++)
{// insert a[i] into a[0:i-1]
int t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = t;
}
Comparison Count
 Pick an instance characteristic … n, n =
a.length for insertion sort
 Determine count as a function of this
instance characteristic.
Comparison Count
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
How many comparisons are made?
Comparison Count
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
number of compares depends on
a[]s and t as well as on i
Comparison Count
 Worst-case count = maximum count
 Best-case count = minimum count
 Average count
Worst-Case Comparison Count
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a = [1, 2, 3, 4] and t = 0 => 4 compares
a = [1,2,3,…,i] and t = 0 => i compares
Worst-Case Comparison Count
for (int i = 1; i < n; i++)
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
total compares = 1 + 2 + 3 + … + (n-1)
= (n-1)n/2
Step Count
A step is an amount of computing that
does not depend on the instance
characteristic n
10 adds, 100 subtracts, 1000 multiplies
can all be counted as a single step
n adds cannot be counted as 1 step
Step Count
for (int i = 1; i < a.length; i++)
{// insert a[i] into a[0:i-1]
int t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = t;
}
for (int i = 1; i < a.length; i++) 1
{// insert a[i] into a[0:i-1] 0
int t = a[i]; 1
int j; 0
for (j = i - 1; j >= 0 && t < a[j]; j--) 1
a[j + 1] = a[j]; 1
a[j + 1] = t; 1
} 0
s/e
Step Count
s/e isn’t always 0 or 1
x = MyMath.sum(a, n);
where n is the instance characteristic
has a s/e count of n
Step Count
for (int i = 1; i < a.length; i++)
{// insert a[i] into a[0:i-1]
int t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = t;
}
for (int i = 1; i < a.length; i++) 1
{// insert a[i] into a[0:i-1] 0
int t = a[i]; 1
int j; 0
for (j = i - 1; j >= 0 && t < a[j]; j--) 1
a[j + 1] = a[j]; 1
a[j + 1] = t; 1
} 0
s/e steps
i
i+ 1
Step Count
for (int i = 1; i < a.length; i++)
{ 2i + 3}
step count for
for (int i = 1; i < a.length; i++)
is n
step count for body of for loop is
2(1+2+3+…+n-1) + 3(n-1)
= (n-1)n + 3(n-1)
= (n-1)(n+3)
Asymptotic Complexity of
Insertion Sort
 O(n2)
 What does this mean?
Complexity of Insertion Sort
 Time or number of operations does not
exceed c.n2 on any input of size n (n
suitably large).
 Actually, the worst-case time is
Theta(n2) and the best-case is Theta(n)
 So, the worst-case time is expected to
quadruple each time n is doubled
Complexity of Insertion Sort
 Is O(n2) too much time?
 Is the algorithm practical?
Practical Complexities
109 instructions/second
n n nlogn n2
n3
1000 1mic 10mic 1milli 1sec
10000 10mic 130mic 100milli 17min
106
1milli 20milli 17min 32years
Impractical Complexities
109 instructions/second
n n4
n10
2n
1000 17min 3.2 x 1013
years
3.2 x 10283
years
10000 116
days
??? ???
106
3 x 107
years
?????? ??????
Faster Computer Vs Better
Algorithm
Algorithmic improvement more useful
than hardware improvement.
E.g. 2n to n3

More Related Content

Similar to lec2.ppt

Lecture 2
Lecture 2Lecture 2
Lecture 2
sajinsc
 
I am trying to implement timing on this program and cannot do it. Wh.pdf
I am trying to implement timing on this program and cannot do it. Wh.pdfI am trying to implement timing on this program and cannot do it. Wh.pdf
I am trying to implement timing on this program and cannot do it. Wh.pdf
allystraders
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
eyewatchsystems
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
웅식 전
 
Please help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdfPlease help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdf
aroramobiles1
 
#include iostream using namespace std; void InsertionSort(int .pdf
#include iostream using namespace std; void InsertionSort(int .pdf#include iostream using namespace std; void InsertionSort(int .pdf
#include iostream using namespace std; void InsertionSort(int .pdf
brijmote
 

Similar to lec2.ppt (20)

Lecture 2
Lecture 2Lecture 2
Lecture 2
 
C programs
C programsC programs
C programs
 
insertion sort.ppt
insertion sort.pptinsertion sort.ppt
insertion sort.ppt
 
I am trying to implement timing on this program and cannot do it. Wh.pdf
I am trying to implement timing on this program and cannot do it. Wh.pdfI am trying to implement timing on this program and cannot do it. Wh.pdf
I am trying to implement timing on this program and cannot do it. Wh.pdf
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Code
CodeCode
Code
 
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdfJava AssignmentWrite a program using sortingsorting bubble,sele.pdf
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
convert the following C code to Mips assembly with steps and comment.pdf
convert the following C code to Mips assembly with steps and comment.pdfconvert the following C code to Mips assembly with steps and comment.pdf
convert the following C code to Mips assembly with steps and comment.pdf
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Recursive squaring
Recursive squaringRecursive squaring
Recursive squaring
 
Dsa sorting
Dsa sortingDsa sorting
Dsa sorting
 
Sorting Technique
Sorting TechniqueSorting Technique
Sorting Technique
 
Please help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdfPlease help with this JAVA Assignment and show output if you can ple.pdf
Please help with this JAVA Assignment and show output if you can ple.pdf
 
for loop in java
for loop in java for loop in java
for loop in java
 
Algorithms with-java-1.0
Algorithms with-java-1.0Algorithms with-java-1.0
Algorithms with-java-1.0
 
Basic_analysis.ppt
Basic_analysis.pptBasic_analysis.ppt
Basic_analysis.ppt
 
#include iostream using namespace std; void InsertionSort(int .pdf
#include iostream using namespace std; void InsertionSort(int .pdf#include iostream using namespace std; void InsertionSort(int .pdf
#include iostream using namespace std; void InsertionSort(int .pdf
 
c++ #include -iostream- using namespace std- void InsertionSort(int nu.pdf
c++ #include -iostream- using namespace std- void InsertionSort(int nu.pdfc++ #include -iostream- using namespace std- void InsertionSort(int nu.pdf
c++ #include -iostream- using namespace std- void InsertionSort(int nu.pdf
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

lec2.ppt

  • 1. Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }
  • 2. Complexity  Space/Memory  Time – Count a particular operation – Count number of steps – Asymptotic complexity
  • 3. Comparison Count for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; }
  • 4. Comparison Count  Pick an instance characteristic … n, n = a.length for insertion sort  Determine count as a function of this instance characteristic.
  • 5. Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; How many comparisons are made?
  • 6. Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; number of compares depends on a[]s and t as well as on i
  • 7. Comparison Count  Worst-case count = maximum count  Best-case count = minimum count  Average count
  • 8. Worst-Case Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a = [1, 2, 3, 4] and t = 0 => 4 compares a = [1,2,3,…,i] and t = 0 => i compares
  • 9. Worst-Case Comparison Count for (int i = 1; i < n; i++) for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; total compares = 1 + 2 + 3 + … + (n-1) = (n-1)n/2
  • 10. Step Count A step is an amount of computing that does not depend on the instance characteristic n 10 adds, 100 subtracts, 1000 multiplies can all be counted as a single step n adds cannot be counted as 1 step
  • 11. Step Count for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; } for (int i = 1; i < a.length; i++) 1 {// insert a[i] into a[0:i-1] 0 int t = a[i]; 1 int j; 0 for (j = i - 1; j >= 0 && t < a[j]; j--) 1 a[j + 1] = a[j]; 1 a[j + 1] = t; 1 } 0 s/e
  • 12. Step Count s/e isn’t always 0 or 1 x = MyMath.sum(a, n); where n is the instance characteristic has a s/e count of n
  • 13. Step Count for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t; } for (int i = 1; i < a.length; i++) 1 {// insert a[i] into a[0:i-1] 0 int t = a[i]; 1 int j; 0 for (j = i - 1; j >= 0 && t < a[j]; j--) 1 a[j + 1] = a[j]; 1 a[j + 1] = t; 1 } 0 s/e steps i i+ 1
  • 14. Step Count for (int i = 1; i < a.length; i++) { 2i + 3} step count for for (int i = 1; i < a.length; i++) is n step count for body of for loop is 2(1+2+3+…+n-1) + 3(n-1) = (n-1)n + 3(n-1) = (n-1)(n+3)
  • 15. Asymptotic Complexity of Insertion Sort  O(n2)  What does this mean?
  • 16. Complexity of Insertion Sort  Time or number of operations does not exceed c.n2 on any input of size n (n suitably large).  Actually, the worst-case time is Theta(n2) and the best-case is Theta(n)  So, the worst-case time is expected to quadruple each time n is doubled
  • 17. Complexity of Insertion Sort  Is O(n2) too much time?  Is the algorithm practical?
  • 18. Practical Complexities 109 instructions/second n n nlogn n2 n3 1000 1mic 10mic 1milli 1sec 10000 10mic 130mic 100milli 17min 106 1milli 20milli 17min 32years
  • 19. Impractical Complexities 109 instructions/second n n4 n10 2n 1000 17min 3.2 x 1013 years 3.2 x 10283 years 10000 116 days ??? ??? 106 3 x 107 years ?????? ??????
  • 20. Faster Computer Vs Better Algorithm Algorithmic improvement more useful than hardware improvement. E.g. 2n to n3

Editor's Notes

  1. Other types of complexity: programming complexity, debugging complexity, complexity of comprehension.
  2. Teraflop computer the 32yr time becomes approx 10 days.