What is the most efficient algorithm to Insert an element in a ordered simple list











up vote
0
down vote

favorite












Which one is the best algorithm to insert a new element in a ordered list?
Supose that I a have the list [1,3,8,10,14,20] and I'd like to insert the element 9.



I'm not talking about a programming language but about the most efficient algorithm to do that.



Tks



ps.: I'm not sure if the insert sort is the best one.










share|improve this question
























  • List or array? These are different things.
    – Henry
    23 hours ago










  • @Henry list. I edited. Tks
    – mr.abdo
    23 hours ago










  • Well, you can use a doubly linked list. This way you won't have to shift elements.
    – vivek_23
    22 hours ago















up vote
0
down vote

favorite












Which one is the best algorithm to insert a new element in a ordered list?
Supose that I a have the list [1,3,8,10,14,20] and I'd like to insert the element 9.



I'm not talking about a programming language but about the most efficient algorithm to do that.



Tks



ps.: I'm not sure if the insert sort is the best one.










share|improve this question
























  • List or array? These are different things.
    – Henry
    23 hours ago










  • @Henry list. I edited. Tks
    – mr.abdo
    23 hours ago










  • Well, you can use a doubly linked list. This way you won't have to shift elements.
    – vivek_23
    22 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Which one is the best algorithm to insert a new element in a ordered list?
Supose that I a have the list [1,3,8,10,14,20] and I'd like to insert the element 9.



I'm not talking about a programming language but about the most efficient algorithm to do that.



Tks



ps.: I'm not sure if the insert sort is the best one.










share|improve this question















Which one is the best algorithm to insert a new element in a ordered list?
Supose that I a have the list [1,3,8,10,14,20] and I'd like to insert the element 9.



I'm not talking about a programming language but about the most efficient algorithm to do that.



Tks



ps.: I'm not sure if the insert sort is the best one.







algorithm sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 23 hours ago

























asked 23 hours ago









mr.abdo

54




54












  • List or array? These are different things.
    – Henry
    23 hours ago










  • @Henry list. I edited. Tks
    – mr.abdo
    23 hours ago










  • Well, you can use a doubly linked list. This way you won't have to shift elements.
    – vivek_23
    22 hours ago


















  • List or array? These are different things.
    – Henry
    23 hours ago










  • @Henry list. I edited. Tks
    – mr.abdo
    23 hours ago










  • Well, you can use a doubly linked list. This way you won't have to shift elements.
    – vivek_23
    22 hours ago
















List or array? These are different things.
– Henry
23 hours ago




List or array? These are different things.
– Henry
23 hours ago












@Henry list. I edited. Tks
– mr.abdo
23 hours ago




@Henry list. I edited. Tks
– mr.abdo
23 hours ago












Well, you can use a doubly linked list. This way you won't have to shift elements.
– vivek_23
22 hours ago




Well, you can use a doubly linked list. This way you won't have to shift elements.
– vivek_23
22 hours ago












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










To insert a single element, you traverse the list from the front until you find the insertion point and then insert the new element (by pointer manipulations).



If you need to insert many elements and the list does not necessarily have to be sorted all the time it may be better to insert the new elements in the front and then do a final sort.






share|improve this answer




























    up vote
    0
    down vote













    Inserting a new element will take approximately the same time no matter the algorithm if its an array. To explain it better:



    You will always need a O(n) complexity part in the algorithm to shift the array elements that come after the inserted element to make space where you can put that element.



    Your best bet would be to decrease the search complexity, and the best way is to use binary search, which has a complexity of O(log n).



    With this your insert has a complexity of (n + log n), which is the same as O(n).



    To increase efficiency, you need to pick a better data structure, that has constant insert time like a binary search tree or a some sort of heap.






    share|improve this answer

















    • 1




      The question is about a list, but apart from that the complexity of binary search is not needed. One can combine shifting up with the search.
      – Henry
      23 hours ago










    • If it's a list, then you are right. Also, in that case, we cant use binary search. But I still stand by the bold part.
      – nihilist_banana
      23 hours ago











    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',
    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%2f53372159%2fwhat-is-the-most-efficient-algorithm-to-insert-an-element-in-a-ordered-simple-li%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    To insert a single element, you traverse the list from the front until you find the insertion point and then insert the new element (by pointer manipulations).



    If you need to insert many elements and the list does not necessarily have to be sorted all the time it may be better to insert the new elements in the front and then do a final sort.






    share|improve this answer

























      up vote
      2
      down vote



      accepted










      To insert a single element, you traverse the list from the front until you find the insertion point and then insert the new element (by pointer manipulations).



      If you need to insert many elements and the list does not necessarily have to be sorted all the time it may be better to insert the new elements in the front and then do a final sort.






      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        To insert a single element, you traverse the list from the front until you find the insertion point and then insert the new element (by pointer manipulations).



        If you need to insert many elements and the list does not necessarily have to be sorted all the time it may be better to insert the new elements in the front and then do a final sort.






        share|improve this answer












        To insert a single element, you traverse the list from the front until you find the insertion point and then insert the new element (by pointer manipulations).



        If you need to insert many elements and the list does not necessarily have to be sorted all the time it may be better to insert the new elements in the front and then do a final sort.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 23 hours ago









        Henry

        32.8k54259




        32.8k54259
























            up vote
            0
            down vote













            Inserting a new element will take approximately the same time no matter the algorithm if its an array. To explain it better:



            You will always need a O(n) complexity part in the algorithm to shift the array elements that come after the inserted element to make space where you can put that element.



            Your best bet would be to decrease the search complexity, and the best way is to use binary search, which has a complexity of O(log n).



            With this your insert has a complexity of (n + log n), which is the same as O(n).



            To increase efficiency, you need to pick a better data structure, that has constant insert time like a binary search tree or a some sort of heap.






            share|improve this answer

















            • 1




              The question is about a list, but apart from that the complexity of binary search is not needed. One can combine shifting up with the search.
              – Henry
              23 hours ago










            • If it's a list, then you are right. Also, in that case, we cant use binary search. But I still stand by the bold part.
              – nihilist_banana
              23 hours ago















            up vote
            0
            down vote













            Inserting a new element will take approximately the same time no matter the algorithm if its an array. To explain it better:



            You will always need a O(n) complexity part in the algorithm to shift the array elements that come after the inserted element to make space where you can put that element.



            Your best bet would be to decrease the search complexity, and the best way is to use binary search, which has a complexity of O(log n).



            With this your insert has a complexity of (n + log n), which is the same as O(n).



            To increase efficiency, you need to pick a better data structure, that has constant insert time like a binary search tree or a some sort of heap.






            share|improve this answer

















            • 1




              The question is about a list, but apart from that the complexity of binary search is not needed. One can combine shifting up with the search.
              – Henry
              23 hours ago










            • If it's a list, then you are right. Also, in that case, we cant use binary search. But I still stand by the bold part.
              – nihilist_banana
              23 hours ago













            up vote
            0
            down vote










            up vote
            0
            down vote









            Inserting a new element will take approximately the same time no matter the algorithm if its an array. To explain it better:



            You will always need a O(n) complexity part in the algorithm to shift the array elements that come after the inserted element to make space where you can put that element.



            Your best bet would be to decrease the search complexity, and the best way is to use binary search, which has a complexity of O(log n).



            With this your insert has a complexity of (n + log n), which is the same as O(n).



            To increase efficiency, you need to pick a better data structure, that has constant insert time like a binary search tree or a some sort of heap.






            share|improve this answer












            Inserting a new element will take approximately the same time no matter the algorithm if its an array. To explain it better:



            You will always need a O(n) complexity part in the algorithm to shift the array elements that come after the inserted element to make space where you can put that element.



            Your best bet would be to decrease the search complexity, and the best way is to use binary search, which has a complexity of O(log n).



            With this your insert has a complexity of (n + log n), which is the same as O(n).



            To increase efficiency, you need to pick a better data structure, that has constant insert time like a binary search tree or a some sort of heap.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 23 hours ago









            nihilist_banana

            15911




            15911








            • 1




              The question is about a list, but apart from that the complexity of binary search is not needed. One can combine shifting up with the search.
              – Henry
              23 hours ago










            • If it's a list, then you are right. Also, in that case, we cant use binary search. But I still stand by the bold part.
              – nihilist_banana
              23 hours ago














            • 1




              The question is about a list, but apart from that the complexity of binary search is not needed. One can combine shifting up with the search.
              – Henry
              23 hours ago










            • If it's a list, then you are right. Also, in that case, we cant use binary search. But I still stand by the bold part.
              – nihilist_banana
              23 hours ago








            1




            1




            The question is about a list, but apart from that the complexity of binary search is not needed. One can combine shifting up with the search.
            – Henry
            23 hours ago




            The question is about a list, but apart from that the complexity of binary search is not needed. One can combine shifting up with the search.
            – Henry
            23 hours ago












            If it's a list, then you are right. Also, in that case, we cant use binary search. But I still stand by the bold part.
            – nihilist_banana
            23 hours ago




            If it's a list, then you are right. Also, in that case, we cant use binary search. But I still stand by the bold part.
            – nihilist_banana
            23 hours ago


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372159%2fwhat-is-the-most-efficient-algorithm-to-insert-an-element-in-a-ordered-simple-li%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

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$