1 / 10

Analysis of ArrayList

Analysis of ArrayList. Adding an element. We denote by n the size of the array when calling the method. Worst case space complexity?. if(not enough space){ create a bigger new array copy the elements } add element at index index++. We create an array of size 2n. 2n is in O(n).

rregister
Download Presentation

Analysis of ArrayList

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Analysis of ArrayList

  2. Adding an element We denote by n the size of the array when calling the method. Worst case space complexity? if(not enough space){ create a bigger new array copy the elements } add element at index index++ We create an array of size 2n 2n is in O(n) Analysis of ArrayList

  3. Adding an element We denote by n the size of the array when calling the method. Worst case time complexity? if(not enough space){ create a bigger new array copy the elements } add element at index index++ We copy all the elements. O(n) Analysis of ArrayList

  4. Accessing an element We denote by n the size of the array when calling the method. Worst case space complexity? It does not consume space so 0. return T[i]; Remember: all constants belong to the class of complexity O(1), so this is still O(1) Analysis of ArrayList

  5. Accessing an element We denote by n the size of the array when calling the method. Worst case time complexity? We get it right away, no intermediate operations needed. return T[i]; O(1) Analysis of ArrayList

  6. Deleting an element We denote by n the size of the array when calling the method. Worst case space complexity? for(int i = target; i<size-1;i++) T[i] = T[i+1] T[size-1]=null; We don’t declare extra variables. O(1) Analysis of ArrayList

  7. Deleting an element We denote by n the size of the array when calling the method. Worst case time complexity? for(int i = target; i<size-1;i++) T[i] = T[i+1] T[size-1]=null; If we delete the first one, we shift all the elements. O(n) Analysis of ArrayList

  8. Searching an element We denote by n the size of the array when calling the method. Worst case space complexity? for(int i = 0; i<size; i++) if(T[i] == e) return true; return false; We don’t declare extra variables. O(1) Analysis of ArrayList

  9. Searching an element We denote by n the size of the array when calling the method. Worst case time complexity? for(int i = 0; i<size; i++) if(T[i] == e) return true; return false; We’ll look through all elements. We saw an optimized methods that delete the comparison, but that’s improving by a factor 2, which does not show up with O notation. O(n) Analysis of ArrayList

  10. Summary Analysis of ArrayList

More Related