Why is deletion of an item at end of Dynamic array O(n) time complexity?












6














I am currently reading my textbook and I am totally confused why a dynamic array would require O(n) time to delete an item at the end. I understand that deleting an item from any other index is O(n) because you have to copy all the data and move them to fill in the gap, but if it’s at the end don’t we simply just decrement the count and set the index to like 0 or null? I included a picture from my book. It’s weird cause it says indexing is O(1) so we must know where the item is so we don’t have to traverse the array like a linked list.enter image description here










share|improve this question






















  • Java does not have a "Dynamic array", so I'm even more confused. There's ArrayList... name and shame! What book?
    – Elliott Frisch
    3 hours ago










  • Data Structures and Algorithms Made Easy in Java
    – Belphegor
    3 hours ago
















6














I am currently reading my textbook and I am totally confused why a dynamic array would require O(n) time to delete an item at the end. I understand that deleting an item from any other index is O(n) because you have to copy all the data and move them to fill in the gap, but if it’s at the end don’t we simply just decrement the count and set the index to like 0 or null? I included a picture from my book. It’s weird cause it says indexing is O(1) so we must know where the item is so we don’t have to traverse the array like a linked list.enter image description here










share|improve this question






















  • Java does not have a "Dynamic array", so I'm even more confused. There's ArrayList... name and shame! What book?
    – Elliott Frisch
    3 hours ago










  • Data Structures and Algorithms Made Easy in Java
    – Belphegor
    3 hours ago














6












6








6


2





I am currently reading my textbook and I am totally confused why a dynamic array would require O(n) time to delete an item at the end. I understand that deleting an item from any other index is O(n) because you have to copy all the data and move them to fill in the gap, but if it’s at the end don’t we simply just decrement the count and set the index to like 0 or null? I included a picture from my book. It’s weird cause it says indexing is O(1) so we must know where the item is so we don’t have to traverse the array like a linked list.enter image description here










share|improve this question













I am currently reading my textbook and I am totally confused why a dynamic array would require O(n) time to delete an item at the end. I understand that deleting an item from any other index is O(n) because you have to copy all the data and move them to fill in the gap, but if it’s at the end don’t we simply just decrement the count and set the index to like 0 or null? I included a picture from my book. It’s weird cause it says indexing is O(1) so we must know where the item is so we don’t have to traverse the array like a linked list.enter image description here







java dynamic-arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 3 hours ago









Belphegor

4601618




4601618












  • Java does not have a "Dynamic array", so I'm even more confused. There's ArrayList... name and shame! What book?
    – Elliott Frisch
    3 hours ago










  • Data Structures and Algorithms Made Easy in Java
    – Belphegor
    3 hours ago


















  • Java does not have a "Dynamic array", so I'm even more confused. There's ArrayList... name and shame! What book?
    – Elliott Frisch
    3 hours ago










  • Data Structures and Algorithms Made Easy in Java
    – Belphegor
    3 hours ago
















Java does not have a "Dynamic array", so I'm even more confused. There's ArrayList... name and shame! What book?
– Elliott Frisch
3 hours ago




Java does not have a "Dynamic array", so I'm even more confused. There's ArrayList... name and shame! What book?
– Elliott Frisch
3 hours ago












Data Structures and Algorithms Made Easy in Java
– Belphegor
3 hours ago




Data Structures and Algorithms Made Easy in Java
– Belphegor
3 hours ago












8 Answers
8






active

oldest

votes


















4














First, let's look up what the books means with a "Dynamic Array":




Dynamic array (also called as growable array, resizable array,
dynamic table, or array list) is a random access, variable-size list data structure that allows elements to be added or removed.
[...]
Note: We will see the implementation for dynamic array in the Stacks, Queues and Hashing chapters.




From this we learn that array lists are examples of a "Dynamic Array" as the author of the book defines it.



But looking further, the book mentioned that:




As soon as that array becomes full, create the new array of size
double than the original array. Similarly, reduce the array size to
half if the elements in the array are less than half
.




(emphasis added by me)



A Java ArrayList doesn't do this - it doesn't decrease in storage when elements are removed. But the author is talking about (or believes that ArrayList does) reduce the array size.
In that case, from a worst-worst-case perspective, you could say that the complexity is O(n) because reducing the size involves copying n elements to the reduced array.



Conclusion:



Although it's not true for Java ArrayList implementations, when the author of this book talks about "dynamic arrays" that "reduce the array size" on deletion when necessary, then the worst-case complexity of a delete at the end of the array is indeed O(n).






share|improve this answer





























    1














    In that case you also have to copy the whole array to a new, larger location. But the cost for this can be charged to the insertions at the end that must have happened for this situation to occur. ... To summarize: Insertion/deletion at position k of an array has amortized complexity O(n - k).






    share|improve this answer

















    • 1




      I’m still not sure what the operations are that happen when you delete an item at the end that makes it O(n)
      – Belphegor
      3 hours ago










    • Look up amortized analysis. The reason, stated inexactly, is that the cost of inserted and deleting includes the cost that at some point the original array overreaches the currently allocated space and you start a new bigger array, say twice the size, and copy all the elements over, and when it gets too small (one quarter of the original) you shrink it.
      – John M I Davis
      2 hours ago



















    0














    As far as I think
    As it is a dynamic array so the computer system does not know as to what is the current length of this dynamic array so to find the length of this dynamic array it takes O(n) time and then takes O(1) time to delete the element at end.






    share|improve this answer








    New contributor




    Mohd Akbar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


























      0














      In java for Dynamic array (ArrayList) time complexity deletion of last element is o(1) in java it does not copy array
      in java they will check weather the array index is end.



        int numMoved = size - index - 1;
      if (numMoved > 0)
      //copy array element





      share|improve this answer





















      • hi @Belphegor as per java they implement as your thought
        – Teja Venkat Lanka
        2 hours ago



















      0














      Deleting an Item from Dynamic array(ArrayList in java) require to Search for the Item and than Delete.



      If the element is at the end of list, than Search itself will result in O(n). I hope this makes sense to you.



      You can view source at http://www.docjar.com/html/api/java/util/ArrayList.java.html






      share|improve this answer





















      • When deleting the last item, you already know it's location. So don't need to be searched. that's why array takes O(1)
        – Janith Kasun
        2 hours ago










      • Index is found base on search O(n) , followed by call remove(index) O(1) , final complexity is O(n). These both operations are combined in remove(item).
        – Noman Khan
        2 hours ago










      • Array indexes are memory references for the direct object, in an array you don't have to search for an element, if you already know the index
        – Janith Kasun
        1 hour ago



















      0














      Insertion and deletion are operations that we generally do not perform on arrays, because they have a fixed length by nature. You cannot increase or decrease the length of something which is fixed by its nature.



      When people speak of "dynamic arrays" in Java they tend to mean using class ArrayList, which is backed by an array, and it provides the illusion of the ability to insert and remove elements.



      But in order for some piece of software to provide the illusion of performing insertions and deletions on an array, each time (or almost each time, there are optimizations possible) it has to allocate a new array of the new desired length, and copy the old array into it, skipping the removed element or adding the inserted element as the case may be. That array copy is where the O(N) comes from.



      And, due to the optimizations performed by ArrayList, the table that you posted is not accurate: it should rather say 'O(1) if the array has not shrunk by much, O(N) if the array has shrunk by so much that reallocation is deemed necessary'. But I guess that would have been too long to fit in the table cell.






      share|improve this answer































        0














        That entry seems like it's either




        1. incorrect, or

        2. true, but misleading.


        You are absolutely right that you can just destroy the object at the final position in a dynamic array and then decrement the size to remove the last element. In many implementations of dynamic arrays, you'll sometimes need to perform resize operations to make sure that the size of the allocated array is within some constant factor of the number of elements. If that happens, then yes, you'll need to make a new array, copy over the old elements, and free the previous array, which does take time O(n). However, you can show that these resizes are sufficiently infrequent that the average cost of doing a remove from the end is O(1). (In a more technical sense, we say that the amortized cost of removing an element from the end is O(1)). That is, as long as you care only about the total cost of performing a series of operations rather than the cost of any individual operation, you would not be wrong to just pretend each operation costs you time O(1).



        I'd say that you're most likely looking at a typo in the materials. Looking at their entry for appending to the end, which differentiates between the not-full and full cases, I think this is likely a copy/paste error. Following the table's lead, that should say something to the effect of "O(1) if the array is not 'too empty,' O(n) otherwise." But again, keep in mind that the amortized efficiency of each operation is O(1), meaning that these scary O(n) terms aren't actually likely to burn you in practice unless you are in a specialized environment where each operation needs to work really quickly.






        share|improve this answer





























          0














          As you have mentioned this could be confusing, if you add an element to a dynamic array, it change its size in a constant interval and will create a new array an copy elements to the new array as you may already know. And when it shrinks in size it will also shirk if needed.



          For an example if interval is 4 when you add 1st, 2nd, 3rd, 4th element, everything will be okay, but when you add the 5th item dynamic array will grow into a 8 elements array and will copy the all elements to the new array.



          It is the same when it is decreasing. If you remove one item from a 5 item array which has an interval of 4, dynamic array it will create a new 4 elements array and copy the elements.



          Here is a good representation video tutorial,



          Yes. When dynamic array does not have to shrink it is O(1) which it takes to remove the element but when it has to shrink its O(n), as you may already figured out.



          when you find the big O notation you are defining the worst case, so it is O(n)






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53939725%2fwhy-is-deletion-of-an-item-at-end-of-dynamic-array-on-time-complexity%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            8 Answers
            8






            active

            oldest

            votes








            8 Answers
            8






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            First, let's look up what the books means with a "Dynamic Array":




            Dynamic array (also called as growable array, resizable array,
            dynamic table, or array list) is a random access, variable-size list data structure that allows elements to be added or removed.
            [...]
            Note: We will see the implementation for dynamic array in the Stacks, Queues and Hashing chapters.




            From this we learn that array lists are examples of a "Dynamic Array" as the author of the book defines it.



            But looking further, the book mentioned that:




            As soon as that array becomes full, create the new array of size
            double than the original array. Similarly, reduce the array size to
            half if the elements in the array are less than half
            .




            (emphasis added by me)



            A Java ArrayList doesn't do this - it doesn't decrease in storage when elements are removed. But the author is talking about (or believes that ArrayList does) reduce the array size.
            In that case, from a worst-worst-case perspective, you could say that the complexity is O(n) because reducing the size involves copying n elements to the reduced array.



            Conclusion:



            Although it's not true for Java ArrayList implementations, when the author of this book talks about "dynamic arrays" that "reduce the array size" on deletion when necessary, then the worst-case complexity of a delete at the end of the array is indeed O(n).






            share|improve this answer


























              4














              First, let's look up what the books means with a "Dynamic Array":




              Dynamic array (also called as growable array, resizable array,
              dynamic table, or array list) is a random access, variable-size list data structure that allows elements to be added or removed.
              [...]
              Note: We will see the implementation for dynamic array in the Stacks, Queues and Hashing chapters.




              From this we learn that array lists are examples of a "Dynamic Array" as the author of the book defines it.



              But looking further, the book mentioned that:




              As soon as that array becomes full, create the new array of size
              double than the original array. Similarly, reduce the array size to
              half if the elements in the array are less than half
              .




              (emphasis added by me)



              A Java ArrayList doesn't do this - it doesn't decrease in storage when elements are removed. But the author is talking about (or believes that ArrayList does) reduce the array size.
              In that case, from a worst-worst-case perspective, you could say that the complexity is O(n) because reducing the size involves copying n elements to the reduced array.



              Conclusion:



              Although it's not true for Java ArrayList implementations, when the author of this book talks about "dynamic arrays" that "reduce the array size" on deletion when necessary, then the worst-case complexity of a delete at the end of the array is indeed O(n).






              share|improve this answer
























                4












                4








                4






                First, let's look up what the books means with a "Dynamic Array":




                Dynamic array (also called as growable array, resizable array,
                dynamic table, or array list) is a random access, variable-size list data structure that allows elements to be added or removed.
                [...]
                Note: We will see the implementation for dynamic array in the Stacks, Queues and Hashing chapters.




                From this we learn that array lists are examples of a "Dynamic Array" as the author of the book defines it.



                But looking further, the book mentioned that:




                As soon as that array becomes full, create the new array of size
                double than the original array. Similarly, reduce the array size to
                half if the elements in the array are less than half
                .




                (emphasis added by me)



                A Java ArrayList doesn't do this - it doesn't decrease in storage when elements are removed. But the author is talking about (or believes that ArrayList does) reduce the array size.
                In that case, from a worst-worst-case perspective, you could say that the complexity is O(n) because reducing the size involves copying n elements to the reduced array.



                Conclusion:



                Although it's not true for Java ArrayList implementations, when the author of this book talks about "dynamic arrays" that "reduce the array size" on deletion when necessary, then the worst-case complexity of a delete at the end of the array is indeed O(n).






                share|improve this answer












                First, let's look up what the books means with a "Dynamic Array":




                Dynamic array (also called as growable array, resizable array,
                dynamic table, or array list) is a random access, variable-size list data structure that allows elements to be added or removed.
                [...]
                Note: We will see the implementation for dynamic array in the Stacks, Queues and Hashing chapters.




                From this we learn that array lists are examples of a "Dynamic Array" as the author of the book defines it.



                But looking further, the book mentioned that:




                As soon as that array becomes full, create the new array of size
                double than the original array. Similarly, reduce the array size to
                half if the elements in the array are less than half
                .




                (emphasis added by me)



                A Java ArrayList doesn't do this - it doesn't decrease in storage when elements are removed. But the author is talking about (or believes that ArrayList does) reduce the array size.
                In that case, from a worst-worst-case perspective, you could say that the complexity is O(n) because reducing the size involves copying n elements to the reduced array.



                Conclusion:



                Although it's not true for Java ArrayList implementations, when the author of this book talks about "dynamic arrays" that "reduce the array size" on deletion when necessary, then the worst-case complexity of a delete at the end of the array is indeed O(n).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                Erwin Bolwidt

                23.6k123857




                23.6k123857

























                    1














                    In that case you also have to copy the whole array to a new, larger location. But the cost for this can be charged to the insertions at the end that must have happened for this situation to occur. ... To summarize: Insertion/deletion at position k of an array has amortized complexity O(n - k).






                    share|improve this answer

















                    • 1




                      I’m still not sure what the operations are that happen when you delete an item at the end that makes it O(n)
                      – Belphegor
                      3 hours ago










                    • Look up amortized analysis. The reason, stated inexactly, is that the cost of inserted and deleting includes the cost that at some point the original array overreaches the currently allocated space and you start a new bigger array, say twice the size, and copy all the elements over, and when it gets too small (one quarter of the original) you shrink it.
                      – John M I Davis
                      2 hours ago
















                    1














                    In that case you also have to copy the whole array to a new, larger location. But the cost for this can be charged to the insertions at the end that must have happened for this situation to occur. ... To summarize: Insertion/deletion at position k of an array has amortized complexity O(n - k).






                    share|improve this answer

















                    • 1




                      I’m still not sure what the operations are that happen when you delete an item at the end that makes it O(n)
                      – Belphegor
                      3 hours ago










                    • Look up amortized analysis. The reason, stated inexactly, is that the cost of inserted and deleting includes the cost that at some point the original array overreaches the currently allocated space and you start a new bigger array, say twice the size, and copy all the elements over, and when it gets too small (one quarter of the original) you shrink it.
                      – John M I Davis
                      2 hours ago














                    1












                    1








                    1






                    In that case you also have to copy the whole array to a new, larger location. But the cost for this can be charged to the insertions at the end that must have happened for this situation to occur. ... To summarize: Insertion/deletion at position k of an array has amortized complexity O(n - k).






                    share|improve this answer












                    In that case you also have to copy the whole array to a new, larger location. But the cost for this can be charged to the insertions at the end that must have happened for this situation to occur. ... To summarize: Insertion/deletion at position k of an array has amortized complexity O(n - k).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 3 hours ago









                    Harshith Raj

                    3888




                    3888








                    • 1




                      I’m still not sure what the operations are that happen when you delete an item at the end that makes it O(n)
                      – Belphegor
                      3 hours ago










                    • Look up amortized analysis. The reason, stated inexactly, is that the cost of inserted and deleting includes the cost that at some point the original array overreaches the currently allocated space and you start a new bigger array, say twice the size, and copy all the elements over, and when it gets too small (one quarter of the original) you shrink it.
                      – John M I Davis
                      2 hours ago














                    • 1




                      I’m still not sure what the operations are that happen when you delete an item at the end that makes it O(n)
                      – Belphegor
                      3 hours ago










                    • Look up amortized analysis. The reason, stated inexactly, is that the cost of inserted and deleting includes the cost that at some point the original array overreaches the currently allocated space and you start a new bigger array, say twice the size, and copy all the elements over, and when it gets too small (one quarter of the original) you shrink it.
                      – John M I Davis
                      2 hours ago








                    1




                    1




                    I’m still not sure what the operations are that happen when you delete an item at the end that makes it O(n)
                    – Belphegor
                    3 hours ago




                    I’m still not sure what the operations are that happen when you delete an item at the end that makes it O(n)
                    – Belphegor
                    3 hours ago












                    Look up amortized analysis. The reason, stated inexactly, is that the cost of inserted and deleting includes the cost that at some point the original array overreaches the currently allocated space and you start a new bigger array, say twice the size, and copy all the elements over, and when it gets too small (one quarter of the original) you shrink it.
                    – John M I Davis
                    2 hours ago




                    Look up amortized analysis. The reason, stated inexactly, is that the cost of inserted and deleting includes the cost that at some point the original array overreaches the currently allocated space and you start a new bigger array, say twice the size, and copy all the elements over, and when it gets too small (one quarter of the original) you shrink it.
                    – John M I Davis
                    2 hours ago











                    0














                    As far as I think
                    As it is a dynamic array so the computer system does not know as to what is the current length of this dynamic array so to find the length of this dynamic array it takes O(n) time and then takes O(1) time to delete the element at end.






                    share|improve this answer








                    New contributor




                    Mohd Akbar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.























                      0














                      As far as I think
                      As it is a dynamic array so the computer system does not know as to what is the current length of this dynamic array so to find the length of this dynamic array it takes O(n) time and then takes O(1) time to delete the element at end.






                      share|improve this answer








                      New contributor




                      Mohd Akbar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





















                        0












                        0








                        0






                        As far as I think
                        As it is a dynamic array so the computer system does not know as to what is the current length of this dynamic array so to find the length of this dynamic array it takes O(n) time and then takes O(1) time to delete the element at end.






                        share|improve this answer








                        New contributor




                        Mohd Akbar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.









                        As far as I think
                        As it is a dynamic array so the computer system does not know as to what is the current length of this dynamic array so to find the length of this dynamic array it takes O(n) time and then takes O(1) time to delete the element at end.







                        share|improve this answer








                        New contributor




                        Mohd Akbar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.









                        share|improve this answer



                        share|improve this answer






                        New contributor




                        Mohd Akbar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.









                        answered 3 hours ago









                        Mohd Akbar

                        112




                        112




                        New contributor




                        Mohd Akbar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.





                        New contributor





                        Mohd Akbar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






                        Mohd Akbar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.























                            0














                            In java for Dynamic array (ArrayList) time complexity deletion of last element is o(1) in java it does not copy array
                            in java they will check weather the array index is end.



                              int numMoved = size - index - 1;
                            if (numMoved > 0)
                            //copy array element





                            share|improve this answer





















                            • hi @Belphegor as per java they implement as your thought
                              – Teja Venkat Lanka
                              2 hours ago
















                            0














                            In java for Dynamic array (ArrayList) time complexity deletion of last element is o(1) in java it does not copy array
                            in java they will check weather the array index is end.



                              int numMoved = size - index - 1;
                            if (numMoved > 0)
                            //copy array element





                            share|improve this answer





















                            • hi @Belphegor as per java they implement as your thought
                              – Teja Venkat Lanka
                              2 hours ago














                            0












                            0








                            0






                            In java for Dynamic array (ArrayList) time complexity deletion of last element is o(1) in java it does not copy array
                            in java they will check weather the array index is end.



                              int numMoved = size - index - 1;
                            if (numMoved > 0)
                            //copy array element





                            share|improve this answer












                            In java for Dynamic array (ArrayList) time complexity deletion of last element is o(1) in java it does not copy array
                            in java they will check weather the array index is end.



                              int numMoved = size - index - 1;
                            if (numMoved > 0)
                            //copy array element






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 hours ago









                            Teja Venkat Lanka

                            165




                            165












                            • hi @Belphegor as per java they implement as your thought
                              – Teja Venkat Lanka
                              2 hours ago


















                            • hi @Belphegor as per java they implement as your thought
                              – Teja Venkat Lanka
                              2 hours ago
















                            hi @Belphegor as per java they implement as your thought
                            – Teja Venkat Lanka
                            2 hours ago




                            hi @Belphegor as per java they implement as your thought
                            – Teja Venkat Lanka
                            2 hours ago











                            0














                            Deleting an Item from Dynamic array(ArrayList in java) require to Search for the Item and than Delete.



                            If the element is at the end of list, than Search itself will result in O(n). I hope this makes sense to you.



                            You can view source at http://www.docjar.com/html/api/java/util/ArrayList.java.html






                            share|improve this answer





















                            • When deleting the last item, you already know it's location. So don't need to be searched. that's why array takes O(1)
                              – Janith Kasun
                              2 hours ago










                            • Index is found base on search O(n) , followed by call remove(index) O(1) , final complexity is O(n). These both operations are combined in remove(item).
                              – Noman Khan
                              2 hours ago










                            • Array indexes are memory references for the direct object, in an array you don't have to search for an element, if you already know the index
                              – Janith Kasun
                              1 hour ago
















                            0














                            Deleting an Item from Dynamic array(ArrayList in java) require to Search for the Item and than Delete.



                            If the element is at the end of list, than Search itself will result in O(n). I hope this makes sense to you.



                            You can view source at http://www.docjar.com/html/api/java/util/ArrayList.java.html






                            share|improve this answer





















                            • When deleting the last item, you already know it's location. So don't need to be searched. that's why array takes O(1)
                              – Janith Kasun
                              2 hours ago










                            • Index is found base on search O(n) , followed by call remove(index) O(1) , final complexity is O(n). These both operations are combined in remove(item).
                              – Noman Khan
                              2 hours ago










                            • Array indexes are memory references for the direct object, in an array you don't have to search for an element, if you already know the index
                              – Janith Kasun
                              1 hour ago














                            0












                            0








                            0






                            Deleting an Item from Dynamic array(ArrayList in java) require to Search for the Item and than Delete.



                            If the element is at the end of list, than Search itself will result in O(n). I hope this makes sense to you.



                            You can view source at http://www.docjar.com/html/api/java/util/ArrayList.java.html






                            share|improve this answer












                            Deleting an Item from Dynamic array(ArrayList in java) require to Search for the Item and than Delete.



                            If the element is at the end of list, than Search itself will result in O(n). I hope this makes sense to you.



                            You can view source at http://www.docjar.com/html/api/java/util/ArrayList.java.html







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 2 hours ago









                            Noman Khan

                            44828




                            44828












                            • When deleting the last item, you already know it's location. So don't need to be searched. that's why array takes O(1)
                              – Janith Kasun
                              2 hours ago










                            • Index is found base on search O(n) , followed by call remove(index) O(1) , final complexity is O(n). These both operations are combined in remove(item).
                              – Noman Khan
                              2 hours ago










                            • Array indexes are memory references for the direct object, in an array you don't have to search for an element, if you already know the index
                              – Janith Kasun
                              1 hour ago


















                            • When deleting the last item, you already know it's location. So don't need to be searched. that's why array takes O(1)
                              – Janith Kasun
                              2 hours ago










                            • Index is found base on search O(n) , followed by call remove(index) O(1) , final complexity is O(n). These both operations are combined in remove(item).
                              – Noman Khan
                              2 hours ago










                            • Array indexes are memory references for the direct object, in an array you don't have to search for an element, if you already know the index
                              – Janith Kasun
                              1 hour ago
















                            When deleting the last item, you already know it's location. So don't need to be searched. that's why array takes O(1)
                            – Janith Kasun
                            2 hours ago




                            When deleting the last item, you already know it's location. So don't need to be searched. that's why array takes O(1)
                            – Janith Kasun
                            2 hours ago












                            Index is found base on search O(n) , followed by call remove(index) O(1) , final complexity is O(n). These both operations are combined in remove(item).
                            – Noman Khan
                            2 hours ago




                            Index is found base on search O(n) , followed by call remove(index) O(1) , final complexity is O(n). These both operations are combined in remove(item).
                            – Noman Khan
                            2 hours ago












                            Array indexes are memory references for the direct object, in an array you don't have to search for an element, if you already know the index
                            – Janith Kasun
                            1 hour ago




                            Array indexes are memory references for the direct object, in an array you don't have to search for an element, if you already know the index
                            – Janith Kasun
                            1 hour ago











                            0














                            Insertion and deletion are operations that we generally do not perform on arrays, because they have a fixed length by nature. You cannot increase or decrease the length of something which is fixed by its nature.



                            When people speak of "dynamic arrays" in Java they tend to mean using class ArrayList, which is backed by an array, and it provides the illusion of the ability to insert and remove elements.



                            But in order for some piece of software to provide the illusion of performing insertions and deletions on an array, each time (or almost each time, there are optimizations possible) it has to allocate a new array of the new desired length, and copy the old array into it, skipping the removed element or adding the inserted element as the case may be. That array copy is where the O(N) comes from.



                            And, due to the optimizations performed by ArrayList, the table that you posted is not accurate: it should rather say 'O(1) if the array has not shrunk by much, O(N) if the array has shrunk by so much that reallocation is deemed necessary'. But I guess that would have been too long to fit in the table cell.






                            share|improve this answer




























                              0














                              Insertion and deletion are operations that we generally do not perform on arrays, because they have a fixed length by nature. You cannot increase or decrease the length of something which is fixed by its nature.



                              When people speak of "dynamic arrays" in Java they tend to mean using class ArrayList, which is backed by an array, and it provides the illusion of the ability to insert and remove elements.



                              But in order for some piece of software to provide the illusion of performing insertions and deletions on an array, each time (or almost each time, there are optimizations possible) it has to allocate a new array of the new desired length, and copy the old array into it, skipping the removed element or adding the inserted element as the case may be. That array copy is where the O(N) comes from.



                              And, due to the optimizations performed by ArrayList, the table that you posted is not accurate: it should rather say 'O(1) if the array has not shrunk by much, O(N) if the array has shrunk by so much that reallocation is deemed necessary'. But I guess that would have been too long to fit in the table cell.






                              share|improve this answer


























                                0












                                0








                                0






                                Insertion and deletion are operations that we generally do not perform on arrays, because they have a fixed length by nature. You cannot increase or decrease the length of something which is fixed by its nature.



                                When people speak of "dynamic arrays" in Java they tend to mean using class ArrayList, which is backed by an array, and it provides the illusion of the ability to insert and remove elements.



                                But in order for some piece of software to provide the illusion of performing insertions and deletions on an array, each time (or almost each time, there are optimizations possible) it has to allocate a new array of the new desired length, and copy the old array into it, skipping the removed element or adding the inserted element as the case may be. That array copy is where the O(N) comes from.



                                And, due to the optimizations performed by ArrayList, the table that you posted is not accurate: it should rather say 'O(1) if the array has not shrunk by much, O(N) if the array has shrunk by so much that reallocation is deemed necessary'. But I guess that would have been too long to fit in the table cell.






                                share|improve this answer














                                Insertion and deletion are operations that we generally do not perform on arrays, because they have a fixed length by nature. You cannot increase or decrease the length of something which is fixed by its nature.



                                When people speak of "dynamic arrays" in Java they tend to mean using class ArrayList, which is backed by an array, and it provides the illusion of the ability to insert and remove elements.



                                But in order for some piece of software to provide the illusion of performing insertions and deletions on an array, each time (or almost each time, there are optimizations possible) it has to allocate a new array of the new desired length, and copy the old array into it, skipping the removed element or adding the inserted element as the case may be. That array copy is where the O(N) comes from.



                                And, due to the optimizations performed by ArrayList, the table that you posted is not accurate: it should rather say 'O(1) if the array has not shrunk by much, O(N) if the array has shrunk by so much that reallocation is deemed necessary'. But I guess that would have been too long to fit in the table cell.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 2 hours ago

























                                answered 2 hours ago









                                Mike Nakis

                                36.9k65592




                                36.9k65592























                                    0














                                    That entry seems like it's either




                                    1. incorrect, or

                                    2. true, but misleading.


                                    You are absolutely right that you can just destroy the object at the final position in a dynamic array and then decrement the size to remove the last element. In many implementations of dynamic arrays, you'll sometimes need to perform resize operations to make sure that the size of the allocated array is within some constant factor of the number of elements. If that happens, then yes, you'll need to make a new array, copy over the old elements, and free the previous array, which does take time O(n). However, you can show that these resizes are sufficiently infrequent that the average cost of doing a remove from the end is O(1). (In a more technical sense, we say that the amortized cost of removing an element from the end is O(1)). That is, as long as you care only about the total cost of performing a series of operations rather than the cost of any individual operation, you would not be wrong to just pretend each operation costs you time O(1).



                                    I'd say that you're most likely looking at a typo in the materials. Looking at their entry for appending to the end, which differentiates between the not-full and full cases, I think this is likely a copy/paste error. Following the table's lead, that should say something to the effect of "O(1) if the array is not 'too empty,' O(n) otherwise." But again, keep in mind that the amortized efficiency of each operation is O(1), meaning that these scary O(n) terms aren't actually likely to burn you in practice unless you are in a specialized environment where each operation needs to work really quickly.






                                    share|improve this answer


























                                      0














                                      That entry seems like it's either




                                      1. incorrect, or

                                      2. true, but misleading.


                                      You are absolutely right that you can just destroy the object at the final position in a dynamic array and then decrement the size to remove the last element. In many implementations of dynamic arrays, you'll sometimes need to perform resize operations to make sure that the size of the allocated array is within some constant factor of the number of elements. If that happens, then yes, you'll need to make a new array, copy over the old elements, and free the previous array, which does take time O(n). However, you can show that these resizes are sufficiently infrequent that the average cost of doing a remove from the end is O(1). (In a more technical sense, we say that the amortized cost of removing an element from the end is O(1)). That is, as long as you care only about the total cost of performing a series of operations rather than the cost of any individual operation, you would not be wrong to just pretend each operation costs you time O(1).



                                      I'd say that you're most likely looking at a typo in the materials. Looking at their entry for appending to the end, which differentiates between the not-full and full cases, I think this is likely a copy/paste error. Following the table's lead, that should say something to the effect of "O(1) if the array is not 'too empty,' O(n) otherwise." But again, keep in mind that the amortized efficiency of each operation is O(1), meaning that these scary O(n) terms aren't actually likely to burn you in practice unless you are in a specialized environment where each operation needs to work really quickly.






                                      share|improve this answer
























                                        0












                                        0








                                        0






                                        That entry seems like it's either




                                        1. incorrect, or

                                        2. true, but misleading.


                                        You are absolutely right that you can just destroy the object at the final position in a dynamic array and then decrement the size to remove the last element. In many implementations of dynamic arrays, you'll sometimes need to perform resize operations to make sure that the size of the allocated array is within some constant factor of the number of elements. If that happens, then yes, you'll need to make a new array, copy over the old elements, and free the previous array, which does take time O(n). However, you can show that these resizes are sufficiently infrequent that the average cost of doing a remove from the end is O(1). (In a more technical sense, we say that the amortized cost of removing an element from the end is O(1)). That is, as long as you care only about the total cost of performing a series of operations rather than the cost of any individual operation, you would not be wrong to just pretend each operation costs you time O(1).



                                        I'd say that you're most likely looking at a typo in the materials. Looking at their entry for appending to the end, which differentiates between the not-full and full cases, I think this is likely a copy/paste error. Following the table's lead, that should say something to the effect of "O(1) if the array is not 'too empty,' O(n) otherwise." But again, keep in mind that the amortized efficiency of each operation is O(1), meaning that these scary O(n) terms aren't actually likely to burn you in practice unless you are in a specialized environment where each operation needs to work really quickly.






                                        share|improve this answer












                                        That entry seems like it's either




                                        1. incorrect, or

                                        2. true, but misleading.


                                        You are absolutely right that you can just destroy the object at the final position in a dynamic array and then decrement the size to remove the last element. In many implementations of dynamic arrays, you'll sometimes need to perform resize operations to make sure that the size of the allocated array is within some constant factor of the number of elements. If that happens, then yes, you'll need to make a new array, copy over the old elements, and free the previous array, which does take time O(n). However, you can show that these resizes are sufficiently infrequent that the average cost of doing a remove from the end is O(1). (In a more technical sense, we say that the amortized cost of removing an element from the end is O(1)). That is, as long as you care only about the total cost of performing a series of operations rather than the cost of any individual operation, you would not be wrong to just pretend each operation costs you time O(1).



                                        I'd say that you're most likely looking at a typo in the materials. Looking at their entry for appending to the end, which differentiates between the not-full and full cases, I think this is likely a copy/paste error. Following the table's lead, that should say something to the effect of "O(1) if the array is not 'too empty,' O(n) otherwise." But again, keep in mind that the amortized efficiency of each operation is O(1), meaning that these scary O(n) terms aren't actually likely to burn you in practice unless you are in a specialized environment where each operation needs to work really quickly.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered 2 hours ago









                                        templatetypedef

                                        261k66663887




                                        261k66663887























                                            0














                                            As you have mentioned this could be confusing, if you add an element to a dynamic array, it change its size in a constant interval and will create a new array an copy elements to the new array as you may already know. And when it shrinks in size it will also shirk if needed.



                                            For an example if interval is 4 when you add 1st, 2nd, 3rd, 4th element, everything will be okay, but when you add the 5th item dynamic array will grow into a 8 elements array and will copy the all elements to the new array.



                                            It is the same when it is decreasing. If you remove one item from a 5 item array which has an interval of 4, dynamic array it will create a new 4 elements array and copy the elements.



                                            Here is a good representation video tutorial,



                                            Yes. When dynamic array does not have to shrink it is O(1) which it takes to remove the element but when it has to shrink its O(n), as you may already figured out.



                                            when you find the big O notation you are defining the worst case, so it is O(n)






                                            share|improve this answer




























                                              0














                                              As you have mentioned this could be confusing, if you add an element to a dynamic array, it change its size in a constant interval and will create a new array an copy elements to the new array as you may already know. And when it shrinks in size it will also shirk if needed.



                                              For an example if interval is 4 when you add 1st, 2nd, 3rd, 4th element, everything will be okay, but when you add the 5th item dynamic array will grow into a 8 elements array and will copy the all elements to the new array.



                                              It is the same when it is decreasing. If you remove one item from a 5 item array which has an interval of 4, dynamic array it will create a new 4 elements array and copy the elements.



                                              Here is a good representation video tutorial,



                                              Yes. When dynamic array does not have to shrink it is O(1) which it takes to remove the element but when it has to shrink its O(n), as you may already figured out.



                                              when you find the big O notation you are defining the worst case, so it is O(n)






                                              share|improve this answer


























                                                0












                                                0








                                                0






                                                As you have mentioned this could be confusing, if you add an element to a dynamic array, it change its size in a constant interval and will create a new array an copy elements to the new array as you may already know. And when it shrinks in size it will also shirk if needed.



                                                For an example if interval is 4 when you add 1st, 2nd, 3rd, 4th element, everything will be okay, but when you add the 5th item dynamic array will grow into a 8 elements array and will copy the all elements to the new array.



                                                It is the same when it is decreasing. If you remove one item from a 5 item array which has an interval of 4, dynamic array it will create a new 4 elements array and copy the elements.



                                                Here is a good representation video tutorial,



                                                Yes. When dynamic array does not have to shrink it is O(1) which it takes to remove the element but when it has to shrink its O(n), as you may already figured out.



                                                when you find the big O notation you are defining the worst case, so it is O(n)






                                                share|improve this answer














                                                As you have mentioned this could be confusing, if you add an element to a dynamic array, it change its size in a constant interval and will create a new array an copy elements to the new array as you may already know. And when it shrinks in size it will also shirk if needed.



                                                For an example if interval is 4 when you add 1st, 2nd, 3rd, 4th element, everything will be okay, but when you add the 5th item dynamic array will grow into a 8 elements array and will copy the all elements to the new array.



                                                It is the same when it is decreasing. If you remove one item from a 5 item array which has an interval of 4, dynamic array it will create a new 4 elements array and copy the elements.



                                                Here is a good representation video tutorial,



                                                Yes. When dynamic array does not have to shrink it is O(1) which it takes to remove the element but when it has to shrink its O(n), as you may already figured out.



                                                when you find the big O notation you are defining the worst case, so it is O(n)







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited 2 hours ago

























                                                answered 2 hours ago









                                                Janith Kasun

                                                1,015314




                                                1,015314






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53939725%2fwhy-is-deletion-of-an-item-at-end-of-dynamic-array-on-time-complexity%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    404 Error Contact Form 7 ajax form submitting

                                                    How to know if a Active Directory user can login interactively

                                                    Refactoring coordinates for Minecraft Pi buildings written in Python