Worst-Case Runtime











up vote
0
down vote

favorite












Question



Line 6 runs T(n/2) times in the worst case.



Line 8 runs T(n/2) times in the worst case.



So, T(n/2) + T(n/2) + d (+ d: for the minor constant time operations)



Using master theorem: 0 < 1 so T(n) = O(n) (Answer: A)



How in the world is the answer D?










share|improve this question


























    up vote
    0
    down vote

    favorite












    Question



    Line 6 runs T(n/2) times in the worst case.



    Line 8 runs T(n/2) times in the worst case.



    So, T(n/2) + T(n/2) + d (+ d: for the minor constant time operations)



    Using master theorem: 0 < 1 so T(n) = O(n) (Answer: A)



    How in the world is the answer D?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Question



      Line 6 runs T(n/2) times in the worst case.



      Line 8 runs T(n/2) times in the worst case.



      So, T(n/2) + T(n/2) + d (+ d: for the minor constant time operations)



      Using master theorem: 0 < 1 so T(n) = O(n) (Answer: A)



      How in the world is the answer D?










      share|improve this question













      Question



      Line 6 runs T(n/2) times in the worst case.



      Line 8 runs T(n/2) times in the worst case.



      So, T(n/2) + T(n/2) + d (+ d: for the minor constant time operations)



      Using master theorem: 0 < 1 so T(n) = O(n) (Answer: A)



      How in the world is the answer D?







      runtime master-theorem






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 4:58









      heskinreaper

      62




      62
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Firstly refrain from using links as they can go dead.
          You could have easily typed the code in the image as I have done below



          def select_number(A, low, high):
          if low == high:
          return A[low]
          mid = (low+high) // 2
          if A[mid] < A[mid+1]:
          return select_number(A, mid+1, high) #line 6
          else:
          return select_number(A, low, mid) #line 8


          The user calls select_number(A, 0, len(A)-1).
          Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.



          You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
          So it is not 2T(n/2), just T(n/2), which is what is the solution in D.






          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',
            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%2f53201788%2fworst-case-runtime%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Firstly refrain from using links as they can go dead.
            You could have easily typed the code in the image as I have done below



            def select_number(A, low, high):
            if low == high:
            return A[low]
            mid = (low+high) // 2
            if A[mid] < A[mid+1]:
            return select_number(A, mid+1, high) #line 6
            else:
            return select_number(A, low, mid) #line 8


            The user calls select_number(A, 0, len(A)-1).
            Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.



            You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
            So it is not 2T(n/2), just T(n/2), which is what is the solution in D.






            share|improve this answer

























              up vote
              0
              down vote













              Firstly refrain from using links as they can go dead.
              You could have easily typed the code in the image as I have done below



              def select_number(A, low, high):
              if low == high:
              return A[low]
              mid = (low+high) // 2
              if A[mid] < A[mid+1]:
              return select_number(A, mid+1, high) #line 6
              else:
              return select_number(A, low, mid) #line 8


              The user calls select_number(A, 0, len(A)-1).
              Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.



              You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
              So it is not 2T(n/2), just T(n/2), which is what is the solution in D.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Firstly refrain from using links as they can go dead.
                You could have easily typed the code in the image as I have done below



                def select_number(A, low, high):
                if low == high:
                return A[low]
                mid = (low+high) // 2
                if A[mid] < A[mid+1]:
                return select_number(A, mid+1, high) #line 6
                else:
                return select_number(A, low, mid) #line 8


                The user calls select_number(A, 0, len(A)-1).
                Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.



                You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
                So it is not 2T(n/2), just T(n/2), which is what is the solution in D.






                share|improve this answer












                Firstly refrain from using links as they can go dead.
                You could have easily typed the code in the image as I have done below



                def select_number(A, low, high):
                if low == high:
                return A[low]
                mid = (low+high) // 2
                if A[mid] < A[mid+1]:
                return select_number(A, mid+1, high) #line 6
                else:
                return select_number(A, low, mid) #line 8


                The user calls select_number(A, 0, len(A)-1).
                Without using the master theorem we can see that any invocation of select_number will make at most only one recursive call to itself, each time reducing the input space by half. This is similar to a binary search so your expected running time should be in log n.



                You are counting the running times of lines 6 and 8 separately whereas they are mutually exclusive: if one runs the other doesn't.
                So it is not 2T(n/2), just T(n/2), which is what is the solution in D.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 5:34









                Siddhesh Rane

                14526




                14526






























                    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%2f53201788%2fworst-case-runtime%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







                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings