SlideShare a Scribd company logo
4
Most read
5
Most read
6
Most read
Justin Kovacich
What are they, exactly?
Time Complexity – The amount of time required to

execute an algorithm
Space Complexity – The amount of memory required
to execute an algorithm.
Big O Notation
Used to describe the amount of time a given

algorithm would take in the worst case, based on the
input size n.
For the sake of analysis, we ignore constants:
O(C * f(n)) = O(g(n)) or O(5N) = O(N)
Algorithm Analysis Time!
void bubblesort(int []array, int len){
boolean unchanged = false;
while(unchanged == false) {
unchanged = true;
for(int i = 0; i < len -1; i++)
if(a[i] > a[i+1]){
swap (a[i], a[i+1])
unchanged = false;
}
}
}
Sample data, lets follow along!
The following represents a sample input array of size n = 6 to our bubble
sort algorithm. This is a look after each pass of the for loop, where it must
go from 0 to n -1.
6

5

4

3

2

1

Totals:

5

4

3

2

1

6

5 swaps

4

3

2

1

5

6

4 swaps +
1 skip

3

2

1

4

5

6

3 swaps +
2 skips

2

1

3

4

5

6

2 swaps +
3 skips

1

2

3

4

5

6

1 swap + 4
skips

1

2

3

4

5

6

5 skips
Time to add it up…
2 + 4(n-1) + 2 + 4(n-2) + 2(i) + … + 2 + 2(n-1)
N loops through while *(N-1 ) loops through for = N 2 –

N
As size of N grows larger, only the N2 factor is
important.
O(f(n)) = O(N2)
The best case for any sort algorithm is O(N), and
bubblesort can achieve that if its data is already
sorted.
On average, it is one of the worse sorting algorithms.
Other Ways to Measure Time Complexity
The Average Case – More difficult to compute

because it requires some knowledge of what you
should expect on average, but is a best measure of an
algorithm. Bubble sort shares the same worst case
time complexity with insertion sort, but on average is
much worse.
The Best Case – Not exactly the best measure of an
algorithm’s performance because unless it is likely to
continually be the best case comparisons between
algorithms are not very meaningful.
A quick look at Space Complexity
In our previous example, our array consisted of an n

integer array, and 3 other variables.
Space complexity is typically a secondary concern to
time complexity given the amount of space in today’s
computers, unless of course its size requirements
simply become too large.
Why is time complexity important?
Allows for comparisons with other algorithms to

determine which is more efficient.
We need a way to determine whether or not
something is going to take a reasonable amount of
time to run or not…Time complexities of 2n are no
good. For n = 100, would be
1267650600228229401496703205376 operations
(which would take a super long time.)
Time Complexity, the bigger
picture.
One of the big questions in Computer Science right

now is the finding a way to determine if an NPComplete problem can be computed in polynomial
time.
NP-Complete problems are problems that cannot, to
our knowledge, be solved in polynomial time, but
whose answer can be verified in polynomial time.
Homework Assignment!
Without any fore-knowledge of the data you’re going

to be operating on, what is the best case time
complexity for a sorting algorithm and why?
References
Dewdney, A.K. The New Turing Omnibus. New York:

Henry Holt, 1989. 96 – 102
“Computational Complexity Theory”, Wikipedia,
http://en.wikipedia.org/wiki/Computational_complexity_t
. Accessed 1/28/08, last modified 1/15/08.

More Related Content

What's hot (20)

PPTX
Asymptotic Notations
Rishabh Soni
 
DOC
Time and space complexity
Ankit Katiyar
 
PPT
Time complexity.ppt
YekoyeTigabuYeko
 
PDF
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
PPT
Time complexity
Katang Isip
 
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
PDF
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
PPTX
Performance analysis(Time & Space Complexity)
swapnac12
 
PPTX
Asymptotic Notation
Protap Mondal
 
PPT
Greedy algorithms
Rajendran
 
PPTX
daa-unit-3-greedy method
hodcsencet
 
PPTX
Algorithm analysis (All in one)
jehan1987
 
PPTX
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Simplilearn
 
PPT
Recursion tree method
Rajendran
 
PDF
Design & Analysis of Algorithms Lecture Notes
FellowBuddy.com
 
PPT
01 knapsack using backtracking
mandlapure
 
PPTX
Stressen's matrix multiplication
Kumar
 
PPT
Data Structures and Algorithm Analysis
Mary Margarat
 
Asymptotic Notations
Rishabh Soni
 
Time and space complexity
Ankit Katiyar
 
Time complexity.ppt
YekoyeTigabuYeko
 
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Time complexity
Katang Isip
 
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
Performance analysis(Time & Space Complexity)
swapnac12
 
Asymptotic Notation
Protap Mondal
 
Greedy algorithms
Rajendran
 
daa-unit-3-greedy method
hodcsencet
 
Algorithm analysis (All in one)
jehan1987
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Simplilearn
 
Recursion tree method
Rajendran
 
Design & Analysis of Algorithms Lecture Notes
FellowBuddy.com
 
01 knapsack using backtracking
mandlapure
 
Stressen's matrix multiplication
Kumar
 
Data Structures and Algorithm Analysis
Mary Margarat
 

Viewers also liked (16)

PPT
Complexity of Algorithm
Muhammad Muzammal
 
PPT
Operational research
Albi Thomas
 
PPT
asymptotic notations i
Ali mahmood
 
PPTX
CPU & RAM
Redoan Ur Rahman
 
PPT
Chapter7 Computer Networks
Muhammad Waqas
 
PPTX
Space complexity
Bhanusree Koduru
 
PDF
Time complexity (linear search vs binary search)
Kumar
 
PPT
how to calclute time complexity of algortihm
Sajid Marwat
 
PPTX
Radix 4 FFT algorithm and it time complexity computation
Raj Jaiswal
 
PDF
CIM/e-Marketing Award-Assignment # 3 and #4 (combined): A Full Fledge e-Marke...
Naja Faysal
 
PDF
WDL - como criar nomes para empresas
We Do Logos
 
PPTX
Building Healthier Communities: TEDMED 2016
Luminary Labs
 
PPTX
Culture
Reed Hastings
 
PDF
TEDx Manchester: AI & The Future of Work
Volker Hirsch
 
PDF
Build Features, Not Apps
Natasha Murashev
 
PDF
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
SlideShare
 
Complexity of Algorithm
Muhammad Muzammal
 
Operational research
Albi Thomas
 
asymptotic notations i
Ali mahmood
 
CPU & RAM
Redoan Ur Rahman
 
Chapter7 Computer Networks
Muhammad Waqas
 
Space complexity
Bhanusree Koduru
 
Time complexity (linear search vs binary search)
Kumar
 
how to calclute time complexity of algortihm
Sajid Marwat
 
Radix 4 FFT algorithm and it time complexity computation
Raj Jaiswal
 
CIM/e-Marketing Award-Assignment # 3 and #4 (combined): A Full Fledge e-Marke...
Naja Faysal
 
WDL - como criar nomes para empresas
We Do Logos
 
Building Healthier Communities: TEDMED 2016
Luminary Labs
 
Culture
Reed Hastings
 
TEDx Manchester: AI & The Future of Work
Volker Hirsch
 
Build Features, Not Apps
Natasha Murashev
 
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
SlideShare
 
Ad

Similar to Time andspacecomplexity (20)

DOCX
Time Complexity Analysis in Data Structure.docx
Mani .S (Specialization in Semantic Web)
 
PPTX
presentationfinal-090714235255-phpapp01 (1) (2).pptx
javed75
 
PPTX
algocomplexity cost effective tradeoff in
javed75
 
PPTX
Ch-2 final exam documet compler design elements
MAHERMOHAMED27
 
PPTX
9. Asymptotic Analysizbbsbsbsbshzhsbbss.pptx
azharkhanofficial345
 
PPTX
Analysis of algorithms
Mallikarjun Biradar
 
PDF
12200223054_SrijanGho;sh_DAA_19.pdfkmkmm
arijitghosal14
 
PPTX
2.03.Asymptotic_analysis.pptx
ssuser1fb3df
 
PPT
Introduction to Algorithms
Venkatesh Iyer
 
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
PPT
Aad introduction
Mr SMAK
 
PPTX
Data Structure Algorithm -Algorithm Complexity
zeeshanhaidermazhar7
 
PPTX
Asymptotic Notations
NagendraK18
 
PPTX
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
PDF
Algorithm Analysis.pdf
MemMem25
 
DOCX
Basic Computer Engineering Unit II as per RGPV Syllabus
NANDINI SHARMA
 
PDF
Data Structure & Algorithms - Mathematical
babuk110
 
PPTX
Module 1 notes of data warehousing and data
vijipersonal2012
 
PPTX
Analysis of algorithms
iqbalphy1
 
PDF
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
TechVision8
 
Time Complexity Analysis in Data Structure.docx
Mani .S (Specialization in Semantic Web)
 
presentationfinal-090714235255-phpapp01 (1) (2).pptx
javed75
 
algocomplexity cost effective tradeoff in
javed75
 
Ch-2 final exam documet compler design elements
MAHERMOHAMED27
 
9. Asymptotic Analysizbbsbsbsbshzhsbbss.pptx
azharkhanofficial345
 
Analysis of algorithms
Mallikarjun Biradar
 
12200223054_SrijanGho;sh_DAA_19.pdfkmkmm
arijitghosal14
 
2.03.Asymptotic_analysis.pptx
ssuser1fb3df
 
Introduction to Algorithms
Venkatesh Iyer
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Aad introduction
Mr SMAK
 
Data Structure Algorithm -Algorithm Complexity
zeeshanhaidermazhar7
 
Asymptotic Notations
NagendraK18
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
Algorithm Analysis.pdf
MemMem25
 
Basic Computer Engineering Unit II as per RGPV Syllabus
NANDINI SHARMA
 
Data Structure & Algorithms - Mathematical
babuk110
 
Module 1 notes of data warehousing and data
vijipersonal2012
 
Analysis of algorithms
iqbalphy1
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
TechVision8
 
Ad

Recently uploaded (20)

PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
community health nursing question paper 2.pdf
Prince kumar
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 

Time andspacecomplexity

  • 2. What are they, exactly? Time Complexity – The amount of time required to execute an algorithm Space Complexity – The amount of memory required to execute an algorithm.
  • 3. Big O Notation Used to describe the amount of time a given algorithm would take in the worst case, based on the input size n. For the sake of analysis, we ignore constants: O(C * f(n)) = O(g(n)) or O(5N) = O(N)
  • 4. Algorithm Analysis Time! void bubblesort(int []array, int len){ boolean unchanged = false; while(unchanged == false) { unchanged = true; for(int i = 0; i < len -1; i++) if(a[i] > a[i+1]){ swap (a[i], a[i+1]) unchanged = false; } } }
  • 5. Sample data, lets follow along! The following represents a sample input array of size n = 6 to our bubble sort algorithm. This is a look after each pass of the for loop, where it must go from 0 to n -1. 6 5 4 3 2 1 Totals: 5 4 3 2 1 6 5 swaps 4 3 2 1 5 6 4 swaps + 1 skip 3 2 1 4 5 6 3 swaps + 2 skips 2 1 3 4 5 6 2 swaps + 3 skips 1 2 3 4 5 6 1 swap + 4 skips 1 2 3 4 5 6 5 skips
  • 6. Time to add it up… 2 + 4(n-1) + 2 + 4(n-2) + 2(i) + … + 2 + 2(n-1) N loops through while *(N-1 ) loops through for = N 2 – N As size of N grows larger, only the N2 factor is important. O(f(n)) = O(N2) The best case for any sort algorithm is O(N), and bubblesort can achieve that if its data is already sorted. On average, it is one of the worse sorting algorithms.
  • 7. Other Ways to Measure Time Complexity The Average Case – More difficult to compute because it requires some knowledge of what you should expect on average, but is a best measure of an algorithm. Bubble sort shares the same worst case time complexity with insertion sort, but on average is much worse. The Best Case – Not exactly the best measure of an algorithm’s performance because unless it is likely to continually be the best case comparisons between algorithms are not very meaningful.
  • 8. A quick look at Space Complexity In our previous example, our array consisted of an n integer array, and 3 other variables. Space complexity is typically a secondary concern to time complexity given the amount of space in today’s computers, unless of course its size requirements simply become too large.
  • 9. Why is time complexity important? Allows for comparisons with other algorithms to determine which is more efficient. We need a way to determine whether or not something is going to take a reasonable amount of time to run or not…Time complexities of 2n are no good. For n = 100, would be 1267650600228229401496703205376 operations (which would take a super long time.)
  • 10. Time Complexity, the bigger picture. One of the big questions in Computer Science right now is the finding a way to determine if an NPComplete problem can be computed in polynomial time. NP-Complete problems are problems that cannot, to our knowledge, be solved in polynomial time, but whose answer can be verified in polynomial time.
  • 11. Homework Assignment! Without any fore-knowledge of the data you’re going to be operating on, what is the best case time complexity for a sorting algorithm and why?
  • 12. References Dewdney, A.K. The New Turing Omnibus. New York: Henry Holt, 1989. 96 – 102 “Computational Complexity Theory”, Wikipedia, http://en.wikipedia.org/wiki/Computational_complexity_t . Accessed 1/28/08, last modified 1/15/08.