Complexity analysis for while loop + list.insert()











up vote
-1
down vote

favorite












I have the following code for merging two sorted arrays in-place:



def mergeArrays(a1,a2,m,n):

i, j, k = m-1, n-1, m+n-1

while i >=0 and j >=0:
if a1[i] >= a2[j]:
i-=1

else:
a1.insert(i+1,a2[j])
j-=1

return a1


where



a = [1, 5, 9, 10, 15, 20] 
b = [2, 3, 8, 13]
m,n = len(a), len(b)


I want to understand what will be the time complexity of the above code. From my limited understanding, the outer while loop will run O(n) times, and list.insert() takes O(n) time to insert an item. So does that make the full program run in O(n*n)?










share|improve this question


























    up vote
    -1
    down vote

    favorite












    I have the following code for merging two sorted arrays in-place:



    def mergeArrays(a1,a2,m,n):

    i, j, k = m-1, n-1, m+n-1

    while i >=0 and j >=0:
    if a1[i] >= a2[j]:
    i-=1

    else:
    a1.insert(i+1,a2[j])
    j-=1

    return a1


    where



    a = [1, 5, 9, 10, 15, 20] 
    b = [2, 3, 8, 13]
    m,n = len(a), len(b)


    I want to understand what will be the time complexity of the above code. From my limited understanding, the outer while loop will run O(n) times, and list.insert() takes O(n) time to insert an item. So does that make the full program run in O(n*n)?










    share|improve this question
























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I have the following code for merging two sorted arrays in-place:



      def mergeArrays(a1,a2,m,n):

      i, j, k = m-1, n-1, m+n-1

      while i >=0 and j >=0:
      if a1[i] >= a2[j]:
      i-=1

      else:
      a1.insert(i+1,a2[j])
      j-=1

      return a1


      where



      a = [1, 5, 9, 10, 15, 20] 
      b = [2, 3, 8, 13]
      m,n = len(a), len(b)


      I want to understand what will be the time complexity of the above code. From my limited understanding, the outer while loop will run O(n) times, and list.insert() takes O(n) time to insert an item. So does that make the full program run in O(n*n)?










      share|improve this question













      I have the following code for merging two sorted arrays in-place:



      def mergeArrays(a1,a2,m,n):

      i, j, k = m-1, n-1, m+n-1

      while i >=0 and j >=0:
      if a1[i] >= a2[j]:
      i-=1

      else:
      a1.insert(i+1,a2[j])
      j-=1

      return a1


      where



      a = [1, 5, 9, 10, 15, 20] 
      b = [2, 3, 8, 13]
      m,n = len(a), len(b)


      I want to understand what will be the time complexity of the above code. From my limited understanding, the outer while loop will run O(n) times, and list.insert() takes O(n) time to insert an item. So does that make the full program run in O(n*n)?







      python complexity






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      ajaanbaahu

      1914




      1914



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f207505%2fcomplexity-analysis-for-while-loop-list-insert%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207505%2fcomplexity-analysis-for-while-loop-list-insert%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Список кардиналов, возведённых папой римским Каликстом III

          Deduzione

          Mysql.sock missing - “Can't connect to local MySQL server through socket”