SlideShare a Scribd company logo
1 of 19
Data Structures
&
Algorithms
Complexity in Array
Array
• Array
– Consecutive group of memory locations
– Same name and type (int, char, etc.)
• To refer to an element
– Specify array name and position number (index)
– Format: array_name[ position number ]
– First element at position 0
• N-element array c
c[ 0 ], c[ 1 ] … c[ n - 1 ]
– Nth element as position N-1
c[6]
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
Name of array (All
elements of this
array have the same
name, c)
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Position number of the
element within array c
Complexity
• The complexity of an algorithm is a function describing the efficiency of the algorithm in
terms of the amount of data the algorithm must process. There are two main complexity
measures of the efficiency of an algorithm:
• Time complexity: is a function describing the amount of time an algorithm takes in terms of
the amount of input to the algorithm.
• Space complexity: is a function describing the amount of memory (space) an algorithm takes
in terms of the amount of input to the algorithm.
Time and space
• To analyze an algorithm means:
– developing a formula for predicting how fast an algorithm is, based on the size of the input
(time complexity), and/or
– developing a formula for predicting how much memory an algorithm requires, based on the
size of the input (space complexity)
• Usually time is our biggest concern
– Most algorithms require a fixed amount of space
Average, best, and worst cases
• Usually we would like to find the average time to perform an algorithm
• However,
– Sometimes the “average” isn’t well defined
• Example: Sorting an “average” array
– Time typically depends on how out of order the array is
– How out of order is the “average” unsorted array?
– Sometimes finding the average is too difficult
• Often we have to be satisfied with finding the worst (longest) time required
– Sometimes this is even what we want (say, for time-critical operations)
• The best (fastest) case is seldom of interest
Common time complexities
BETTER
WORSE
• O(1) constant time
• O(log n) log time
• O(n) linear time
• O(n log n) log linear time
• O(n2) quadratic time
• O(n3) cubic time
• O(nk) polynomial time
• O(2n) exponential time
Space Complexity
• Space complexity : The amount of memory required by an algorithm to
run to completion
– the most often encountered cause is “memory leaks” – the amount of
memory required larger than the memory available on a given system
• Some algorithms may be more efficient if data completely loaded into
memory
– Need to look also at ‘system limitations’
– e.g. Classify 2GB of text in various categories – can we afford to load
the entire collection?
Array Operations
Following are the basic operations supported by an array.
• Traverse − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the value.
• Update − Updates an element at the given index.
• Sorting − Arranging the elements in some type of order.
Array Traverse
Time complexity in Traverse
In array the individual elements are typically stored in consecutive memory locations.
So the complexity is O(1).
It is both for average and worst case.
 Begin at the first element.
 Repeat until there are no more
elements.
 Process the current element.
 Move to the next element.
First Insert
• Adding an element to beginning of an array is O(n) - it would require to
shift all the existing elements by one position.
• All elements in an array list are stored in a contiguous memory location. If
we add more elements than the current size of the array - it will be grown
automatically to accommodate the new element.
Time complexity in First Insert
 Insertion of an element in the first position of
an array.
 Need to shift all the existing elements by one
position.
Last Insert
 Insertion of an element in the last
position of an array.
 No need to shift all the existing
elements by one position.
Time complexity in Last Insert
• Addition to the end is O(1) amortized over multiple insertions for unsorted
array.
• O(n) for sorted array.
Before Insert
• Adding an element before the particular position of an array is O(n) - it
would require to shift the existing elements by one position which are
after the desired position.
• Same both for average and worst case.
 Insertion of an element before the desired
position of a array.
 Need to shift the existing elements by one
position which are after the desired
position.
Time complexity in Before Insert
Element 4 is to be inserted before 6
After Insert
• Adding an element before the particular position of an array is O(n) - it
would require to shift the existing elements by one position which are
after the desired position.
• Same both for average, and worst case.
 Need Insertion of an element after the desired
position of an array.
 Need to shift the existing elements by one
position which are after the desired position.
Time complexity in Before Insert
Element 4 is to be inserted after 3
First Delete
 Deletion of an element from the
first position of an array.
 Need to move everything after
the deleted element back one
space to the left.
Time complexity in First Delete
• Delete from the first position, we'll have to move all n-1 remaining
elements over, giving us O(n) for our worst case.
• Same both for average, and worst case.
Last Delete
 Deletion of an element from the
last position of an array.
 No need to move everything after
the deleted element back one
space to the left.
Time complexity in Last Delete
• Delete from the last position, that just takes constant time.
• O(1) for unsorted array.
• O(n) for sorted array.
Particular Delete
 Deletion of an element from the
particular position of an array.
 Need to move everything after the
deleted element back one space to
the left.
Time complexity in Particular Delete
• Delete from the particular position, we'll have to move all n-1 remaining
elements over, giving us O(n) for our worst case.
• Same both for average, and worst case.
Element 3 is to be deleted
Before Delete
• Delete before the particular position, we'll have to move all n-1 remaining
elements over, giving us O(n) for our worst case.
• Same both for average, and worst case.
 Deletion of an element before the
particular position of an array.
 Need to move everything after the
deleted element back one space to
the left.
Element before 6 is to be deleted
Time complexity in Before Delete
After Delete
• Delete after the particular position, we'll have to move all n-1 remaining
elements over, giving us O(n) for our worst case.
• Same both for average, and worst case.
 Deletion of an element after the
particular position of an array.
 Need to move everything after the
deleted element back one space to
the left.
Element after 2 is to be deleted
Time complexity in After Delete
Searching Algorithms , Complexity
Linear search
Class Search algorithm
Data structure Array
Worst-case performance O(n)
Best-case performance O(1)
Average performance O(n)
Worst-case space complexity O(1) iterative
Binary search
Class Search algorithm
Data structure Array
Worst-case performance O(log n)
Best-case performance O(1)
Average performance O(log n)
Worst-case space complexity O(1)
Array Sorting Algorithms , Complexity
Algorithms Time Complexity Space Complexity
Best Average Worst Worst
Quick sort O(n log(n)) O(n log(n)) O(n^2) O(log(n))
Merge sort O(n log(n)) O(n log(n)) O(n log(n)) O(n)
Tim sort O(n) O(n log(n)) O(n log(n)) O(n)
Heap sort O(n log(n)) O(n log(n)) O(n log(n)) O(1)
Bubble Sort O(n) O(n^2) O(n^2) O(1)
Insertion Sort O(n) O(n^2) O(n^2) O(1)
Selection Sort O(n^2) O(n^2) O(n^2) O(1)
Tree Sort O(n log(n)) O(n log(n)) O(n^2) O(n)
Shell Sort O(n log(n)) O(n(log(n))^2) O(n(log(n))^2) O(1)
Bucket Sort O(n+k) O(n+k) O(n^2) O(n)
Radix Sort O(nk) O(nk) O(nk) O(n+k)
Counting Sort O(n+k) Θ(n+k) O(n+k) O(k)
Cube sort O(n) Θ(n log(n)) O(n log(n)) O(n)
At a Glance
Data
Structure
Time Complexity
Space
Complexity
Average Worst Worst
Access Search Insertion Deletion Access Search Insertion Deletion
Array O(1) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n)
Data Structure Insert Delete Balance
Get at
index
Search
Find
minimum
Find
maximum
Space
usage
Unsorted array O(1) O(1) N/A O(1) O(n) O(n) O(n) O(n)
Sorted array O(n) O(n) N/A O(1) O(log n) O(1) O(1) O(n)

More Related Content

What's hot

What's hot (20)

heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Binary search
Binary searchBinary search
Binary search
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Array sorting
Array sortingArray sorting
Array sorting
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
Linked List
Linked ListLinked List
Linked List
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Linked list
Linked listLinked list
Linked list
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Sorting
SortingSorting
Sorting
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 

Similar to Complexity in array

Similar to Complexity in array (20)

queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Sorting
SortingSorting
Sorting
 
DS Module1 (1).pptx
DS Module1 (1).pptxDS Module1 (1).pptx
DS Module1 (1).pptx
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
CSPC/ PPS Sorting methods
CSPC/ PPS Sorting methodsCSPC/ PPS Sorting methods
CSPC/ PPS Sorting methods
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Queue
QueueQueue
Queue
 
Ch2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxCh2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptx
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
Sorting
SortingSorting
Sorting
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 

Recently uploaded

VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 

Recently uploaded (20)

Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Decoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in ActionDecoding Loan Approval: Predictive Modeling in Action
Decoding Loan Approval: Predictive Modeling in Action
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 

Complexity in array

  • 2. Array • Array – Consecutive group of memory locations – Same name and type (int, char, etc.) • To refer to an element – Specify array name and position number (index) – Format: array_name[ position number ] – First element at position 0 • N-element array c c[ 0 ], c[ 1 ] … c[ n - 1 ] – Nth element as position N-1 c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Name of array (All elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c
  • 3. Complexity • The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process. There are two main complexity measures of the efficiency of an algorithm: • Time complexity: is a function describing the amount of time an algorithm takes in terms of the amount of input to the algorithm. • Space complexity: is a function describing the amount of memory (space) an algorithm takes in terms of the amount of input to the algorithm. Time and space • To analyze an algorithm means: – developing a formula for predicting how fast an algorithm is, based on the size of the input (time complexity), and/or – developing a formula for predicting how much memory an algorithm requires, based on the size of the input (space complexity) • Usually time is our biggest concern – Most algorithms require a fixed amount of space
  • 4. Average, best, and worst cases • Usually we would like to find the average time to perform an algorithm • However, – Sometimes the “average” isn’t well defined • Example: Sorting an “average” array – Time typically depends on how out of order the array is – How out of order is the “average” unsorted array? – Sometimes finding the average is too difficult • Often we have to be satisfied with finding the worst (longest) time required – Sometimes this is even what we want (say, for time-critical operations) • The best (fastest) case is seldom of interest Common time complexities BETTER WORSE • O(1) constant time • O(log n) log time • O(n) linear time • O(n log n) log linear time • O(n2) quadratic time • O(n3) cubic time • O(nk) polynomial time • O(2n) exponential time
  • 5. Space Complexity • Space complexity : The amount of memory required by an algorithm to run to completion – the most often encountered cause is “memory leaks” – the amount of memory required larger than the memory available on a given system • Some algorithms may be more efficient if data completely loaded into memory – Need to look also at ‘system limitations’ – e.g. Classify 2GB of text in various categories – can we afford to load the entire collection?
  • 6. Array Operations Following are the basic operations supported by an array. • Traverse − print all the array elements one by one. • Insertion − Adds an element at the given index. • Deletion − Deletes an element at the given index. • Search − Searches an element using the given index or by the value. • Update − Updates an element at the given index. • Sorting − Arranging the elements in some type of order.
  • 7. Array Traverse Time complexity in Traverse In array the individual elements are typically stored in consecutive memory locations. So the complexity is O(1). It is both for average and worst case.  Begin at the first element.  Repeat until there are no more elements.  Process the current element.  Move to the next element.
  • 8. First Insert • Adding an element to beginning of an array is O(n) - it would require to shift all the existing elements by one position. • All elements in an array list are stored in a contiguous memory location. If we add more elements than the current size of the array - it will be grown automatically to accommodate the new element. Time complexity in First Insert  Insertion of an element in the first position of an array.  Need to shift all the existing elements by one position.
  • 9. Last Insert  Insertion of an element in the last position of an array.  No need to shift all the existing elements by one position. Time complexity in Last Insert • Addition to the end is O(1) amortized over multiple insertions for unsorted array. • O(n) for sorted array.
  • 10. Before Insert • Adding an element before the particular position of an array is O(n) - it would require to shift the existing elements by one position which are after the desired position. • Same both for average and worst case.  Insertion of an element before the desired position of a array.  Need to shift the existing elements by one position which are after the desired position. Time complexity in Before Insert Element 4 is to be inserted before 6
  • 11. After Insert • Adding an element before the particular position of an array is O(n) - it would require to shift the existing elements by one position which are after the desired position. • Same both for average, and worst case.  Need Insertion of an element after the desired position of an array.  Need to shift the existing elements by one position which are after the desired position. Time complexity in Before Insert Element 4 is to be inserted after 3
  • 12. First Delete  Deletion of an element from the first position of an array.  Need to move everything after the deleted element back one space to the left. Time complexity in First Delete • Delete from the first position, we'll have to move all n-1 remaining elements over, giving us O(n) for our worst case. • Same both for average, and worst case.
  • 13. Last Delete  Deletion of an element from the last position of an array.  No need to move everything after the deleted element back one space to the left. Time complexity in Last Delete • Delete from the last position, that just takes constant time. • O(1) for unsorted array. • O(n) for sorted array.
  • 14. Particular Delete  Deletion of an element from the particular position of an array.  Need to move everything after the deleted element back one space to the left. Time complexity in Particular Delete • Delete from the particular position, we'll have to move all n-1 remaining elements over, giving us O(n) for our worst case. • Same both for average, and worst case. Element 3 is to be deleted
  • 15. Before Delete • Delete before the particular position, we'll have to move all n-1 remaining elements over, giving us O(n) for our worst case. • Same both for average, and worst case.  Deletion of an element before the particular position of an array.  Need to move everything after the deleted element back one space to the left. Element before 6 is to be deleted Time complexity in Before Delete
  • 16. After Delete • Delete after the particular position, we'll have to move all n-1 remaining elements over, giving us O(n) for our worst case. • Same both for average, and worst case.  Deletion of an element after the particular position of an array.  Need to move everything after the deleted element back one space to the left. Element after 2 is to be deleted Time complexity in After Delete
  • 17. Searching Algorithms , Complexity Linear search Class Search algorithm Data structure Array Worst-case performance O(n) Best-case performance O(1) Average performance O(n) Worst-case space complexity O(1) iterative Binary search Class Search algorithm Data structure Array Worst-case performance O(log n) Best-case performance O(1) Average performance O(log n) Worst-case space complexity O(1)
  • 18. Array Sorting Algorithms , Complexity Algorithms Time Complexity Space Complexity Best Average Worst Worst Quick sort O(n log(n)) O(n log(n)) O(n^2) O(log(n)) Merge sort O(n log(n)) O(n log(n)) O(n log(n)) O(n) Tim sort O(n) O(n log(n)) O(n log(n)) O(n) Heap sort O(n log(n)) O(n log(n)) O(n log(n)) O(1) Bubble Sort O(n) O(n^2) O(n^2) O(1) Insertion Sort O(n) O(n^2) O(n^2) O(1) Selection Sort O(n^2) O(n^2) O(n^2) O(1) Tree Sort O(n log(n)) O(n log(n)) O(n^2) O(n) Shell Sort O(n log(n)) O(n(log(n))^2) O(n(log(n))^2) O(1) Bucket Sort O(n+k) O(n+k) O(n^2) O(n) Radix Sort O(nk) O(nk) O(nk) O(n+k) Counting Sort O(n+k) Θ(n+k) O(n+k) O(k) Cube sort O(n) Θ(n log(n)) O(n log(n)) O(n)
  • 19. At a Glance Data Structure Time Complexity Space Complexity Average Worst Worst Access Search Insertion Deletion Access Search Insertion Deletion Array O(1) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n) Data Structure Insert Delete Balance Get at index Search Find minimum Find maximum Space usage Unsorted array O(1) O(1) N/A O(1) O(n) O(n) O(n) O(n) Sorted array O(n) O(n) N/A O(1) O(log n) O(1) O(1) O(n)