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

Viewers also liked

Presentation1ข้อมูลและสารสนเทศ
Presentation1ข้อมูลและสารสนเทศPresentation1ข้อมูลและสารสนเทศ
Presentation1ข้อมูลและสารสนเทศ
nutoon
 
творець геометрії чисел — георгій вороний
творець геометрії чисел — георгій воронийтворець геометрії чисел — георгій вороний
творець геометрії чисел — георгій вороний
ivmamon2
 
східноєвропейський національний університет воробей назарій, 1 б
східноєвропейський національний університет воробей назарій, 1 бсхідноєвропейський національний університет воробей назарій, 1 б
східноєвропейський національний університет воробей назарій, 1 б
svinchuk
 
Грінченко О. І. Формування географічної компетентності учнів на уроках геогра...
Грінченко О. І. Формування географічної компетентності учнів на уроках геогра...Грінченко О. І. Формування географічної компетентності учнів на уроках геогра...
Грінченко О. І. Формування географічної компетентності учнів на уроках геогра...
Електронні книги Ранок
 

Viewers also liked (15)

Presentation1ข้อมูลและสารสนเทศ
Presentation1ข้อมูลและสารสนเทศPresentation1ข้อมูลและสารสนเทศ
Presentation1ข้อมูลและสารสนเทศ
 
Y Media Generation
Y Media Generation Y Media Generation
Y Media Generation
 
Bank Director List of Worries
Bank Director List of WorriesBank Director List of Worries
Bank Director List of Worries
 
2015 Fintech Distrubtion Survey
2015 Fintech Distrubtion Survey2015 Fintech Distrubtion Survey
2015 Fintech Distrubtion Survey
 
творець геометрії чисел — георгій вороний
творець геометрії чисел — георгій воронийтворець геометрії чисел — георгій вороний
творець геометрії чисел — георгій вороний
 
Особенности проведения ГИА-2016
Особенности проведения ГИА-2016Особенности проведения ГИА-2016
Особенности проведения ГИА-2016
 
Полтавська область
Полтавська областьПолтавська область
Полтавська область
 
The Gamification of...Everything?
The Gamification of...Everything?The Gamification of...Everything?
The Gamification of...Everything?
 
Air borne wind energy system (AWES)
Air borne wind energy system (AWES)Air borne wind energy system (AWES)
Air borne wind energy system (AWES)
 
східноєвропейський національний університет воробей назарій, 1 б
східноєвропейський національний університет воробей назарій, 1 бсхідноєвропейський національний університет воробей назарій, 1 б
східноєвропейський національний університет воробей назарій, 1 б
 
Грінченко О. І. Формування географічної компетентності учнів на уроках геогра...
Грінченко О. І. Формування географічної компетентності учнів на уроках геогра...Грінченко О. І. Формування географічної компетентності учнів на уроках геогра...
Грінченко О. І. Формування географічної компетентності учнів на уроках геогра...
 
Az iskola világának narratívái: elmélet és gyakorlat a pedagógiában
Az iskola világának narratívái: elmélet és gyakorlat a pedagógiában Az iskola világának narratívái: elmélet és gyakorlat a pedagógiában
Az iskola világának narratívái: elmélet és gyakorlat a pedagógiában
 
Oktatástervezés, az oktatási folyamat
Oktatástervezés, az oktatási folyamatOktatástervezés, az oktatási folyamat
Oktatástervezés, az oktatási folyamat
 
Phrasal verbs vladimir.cubillos
Phrasal verbs vladimir.cubillosPhrasal verbs vladimir.cubillos
Phrasal verbs vladimir.cubillos
 
Nifty index
Nifty indexNifty index
Nifty index
 

Similar to Engineering ppt by venay magen

Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
apexelectronices01
 
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
웅식 전
 

Similar to Engineering ppt by venay magen (20)

lec2.ppt
lec2.pptlec2.ppt
lec2.ppt
 
lec2.ppt
lec2.pptlec2.ppt
lec2.ppt
 
y Bookmarks People Window Helo Online Derivative edusubmi tionassig.docx
 y Bookmarks People Window Helo Online Derivative edusubmi tionassig.docx y Bookmarks People Window Helo Online Derivative edusubmi tionassig.docx
y Bookmarks People Window Helo Online Derivative edusubmi tionassig.docx
 
C programs
C programsC programs
C programs
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
Arrays
ArraysArrays
Arrays
 
C programs
C programsC programs
C programs
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
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
 
insertion sort.ppt
insertion sort.pptinsertion sort.ppt
insertion sort.ppt
 
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
 
Sorting Technique
Sorting TechniqueSorting Technique
Sorting Technique
 
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
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 

Recently uploaded

Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
Call Girls In Nangloi Rly Metro ꧂…….95996 … 13876 Enjoy ꧂Escort
Call Girls In Nangloi Rly Metro ꧂…….95996 … 13876 Enjoy ꧂EscortCall Girls In Nangloi Rly Metro ꧂…….95996 … 13876 Enjoy ꧂Escort
Call Girls In Nangloi Rly Metro ꧂…….95996 … 13876 Enjoy ꧂Escort
dlhescort
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
lizamodels9
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
daisycvs
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Sheetaleventcompany
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Anamikakaur10
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
amitlee9823
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
lizamodels9
 

Recently uploaded (20)

Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Call Girls In Nangloi Rly Metro ꧂…….95996 … 13876 Enjoy ꧂Escort
Call Girls In Nangloi Rly Metro ꧂…….95996 … 13876 Enjoy ꧂EscortCall Girls In Nangloi Rly Metro ꧂…….95996 … 13876 Enjoy ꧂Escort
Call Girls In Nangloi Rly Metro ꧂…….95996 … 13876 Enjoy ꧂Escort
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
Business Model Canvas (BMC)- A new venture concept
Business Model Canvas (BMC)-  A new venture conceptBusiness Model Canvas (BMC)-  A new venture concept
Business Model Canvas (BMC)- A new venture concept
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
 
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort ServiceEluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Eluru Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptx
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
 
Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
 
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
 
Value Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsValue Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and pains
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperity
 

Engineering ppt by venay magen

  • 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.