Mergesort in Python optimization

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I am currently writing mergesort in Python and have implemented it to work. I have noticed, though, that it is very slow compared to the standard sorted() method. I understand it will always be faster but I am open to suggestions in terms of optimization.



def merge(left,right):
sortedArray=
l_idx,r_idx=0,0 #set index for left and right array
#compare values of left and right array
while l_idx<len(left) and r_idx<len(right):
if(left[l_idx] < right[r_idx]):
sortedArray.append(left[l_idx])
l_idx+=1
else:
sortedArray.append(right[r_idx])
r_idx+=1
if l_idx==len(left):
sortedArray.extend(right[r_idx:])
else:
sortedArray.extend(left[l_idx:])
return sortedArray

def mergesort(A):
if len(A)<=1:
return A
middle=len(A)//2
left,right=mergesort(A[:middle]),mergesort(A[middle:])
return merge(left,right)


Thank you for any feedback







share|improve this question



























    up vote
    2
    down vote

    favorite












    I am currently writing mergesort in Python and have implemented it to work. I have noticed, though, that it is very slow compared to the standard sorted() method. I understand it will always be faster but I am open to suggestions in terms of optimization.



    def merge(left,right):
    sortedArray=
    l_idx,r_idx=0,0 #set index for left and right array
    #compare values of left and right array
    while l_idx<len(left) and r_idx<len(right):
    if(left[l_idx] < right[r_idx]):
    sortedArray.append(left[l_idx])
    l_idx+=1
    else:
    sortedArray.append(right[r_idx])
    r_idx+=1
    if l_idx==len(left):
    sortedArray.extend(right[r_idx:])
    else:
    sortedArray.extend(left[l_idx:])
    return sortedArray

    def mergesort(A):
    if len(A)<=1:
    return A
    middle=len(A)//2
    left,right=mergesort(A[:middle]),mergesort(A[middle:])
    return merge(left,right)


    Thank you for any feedback







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am currently writing mergesort in Python and have implemented it to work. I have noticed, though, that it is very slow compared to the standard sorted() method. I understand it will always be faster but I am open to suggestions in terms of optimization.



      def merge(left,right):
      sortedArray=
      l_idx,r_idx=0,0 #set index for left and right array
      #compare values of left and right array
      while l_idx<len(left) and r_idx<len(right):
      if(left[l_idx] < right[r_idx]):
      sortedArray.append(left[l_idx])
      l_idx+=1
      else:
      sortedArray.append(right[r_idx])
      r_idx+=1
      if l_idx==len(left):
      sortedArray.extend(right[r_idx:])
      else:
      sortedArray.extend(left[l_idx:])
      return sortedArray

      def mergesort(A):
      if len(A)<=1:
      return A
      middle=len(A)//2
      left,right=mergesort(A[:middle]),mergesort(A[middle:])
      return merge(left,right)


      Thank you for any feedback







      share|improve this question













      I am currently writing mergesort in Python and have implemented it to work. I have noticed, though, that it is very slow compared to the standard sorted() method. I understand it will always be faster but I am open to suggestions in terms of optimization.



      def merge(left,right):
      sortedArray=
      l_idx,r_idx=0,0 #set index for left and right array
      #compare values of left and right array
      while l_idx<len(left) and r_idx<len(right):
      if(left[l_idx] < right[r_idx]):
      sortedArray.append(left[l_idx])
      l_idx+=1
      else:
      sortedArray.append(right[r_idx])
      r_idx+=1
      if l_idx==len(left):
      sortedArray.extend(right[r_idx:])
      else:
      sortedArray.extend(left[l_idx:])
      return sortedArray

      def mergesort(A):
      if len(A)<=1:
      return A
      middle=len(A)//2
      left,right=mergesort(A[:middle]),mergesort(A[middle:])
      return merge(left,right)


      Thank you for any feedback









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 23 at 13:12
























      asked Jun 23 at 12:22









      Michael D

      162




      162

























          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: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f197118%2fmergesort-in-python-optimization%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%2f197118%2fmergesort-in-python-optimization%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation