Improving the linear search technique?











up vote
-3
down vote

favorite












I'm afraid we have a bit of a sadistic instructor on our hand, because I honestly did not understand this question and never found the answer in the text book, nor searching the internet.



"2. If the list is already in sorted order, how can you utilize this information in improving the linear search technique? Show your improvement in the algorithm of linear search."



As far as I know, Linear Search have only one job, which is examining each element sequentially. HOW can it be improved? What I know is the Linear Search Big-O notation is O(n) and that O(n) is the worse case performance since the element is placed the very last in the list or array. I was about to answer that question by switching to binary search but then I found that binary search would take even longer since it'll take n/2 times to locate, n/4, n/8 and do on and linear search would take as much time.



Let me know if I'm getting this question the wrong way or I'm missing something about this. Appreciate your help.










share|improve this question


















  • 1




    You can choose a different starting point
    – leonardkraemer
    2 days ago










  • If it is a linked list then locating the middle element really needs O(n/2). For an ArrayList it would be just O(1)
    – Michael Butscher
    2 days ago










  • I did, and as I said above, Binary search would be slower for slower for sorted list.
    – user9578589
    2 days ago






  • 2




    You have to specify the data-structure that you are searching in and all other factors, otherwise trying to optimize is pointless.
    – leonardkraemer
    2 days ago










  • A math question: is the list numerical and is it bound by the range of integers? That is to say, there is a clever math approach that can be taken if the values you're sorting against are known to be numerical only (e.g. no lexicographical strings or decimals) which can be used to optimize the approach.
    – Makoto
    2 days ago















up vote
-3
down vote

favorite












I'm afraid we have a bit of a sadistic instructor on our hand, because I honestly did not understand this question and never found the answer in the text book, nor searching the internet.



"2. If the list is already in sorted order, how can you utilize this information in improving the linear search technique? Show your improvement in the algorithm of linear search."



As far as I know, Linear Search have only one job, which is examining each element sequentially. HOW can it be improved? What I know is the Linear Search Big-O notation is O(n) and that O(n) is the worse case performance since the element is placed the very last in the list or array. I was about to answer that question by switching to binary search but then I found that binary search would take even longer since it'll take n/2 times to locate, n/4, n/8 and do on and linear search would take as much time.



Let me know if I'm getting this question the wrong way or I'm missing something about this. Appreciate your help.










share|improve this question


















  • 1




    You can choose a different starting point
    – leonardkraemer
    2 days ago










  • If it is a linked list then locating the middle element really needs O(n/2). For an ArrayList it would be just O(1)
    – Michael Butscher
    2 days ago










  • I did, and as I said above, Binary search would be slower for slower for sorted list.
    – user9578589
    2 days ago






  • 2




    You have to specify the data-structure that you are searching in and all other factors, otherwise trying to optimize is pointless.
    – leonardkraemer
    2 days ago










  • A math question: is the list numerical and is it bound by the range of integers? That is to say, there is a clever math approach that can be taken if the values you're sorting against are known to be numerical only (e.g. no lexicographical strings or decimals) which can be used to optimize the approach.
    – Makoto
    2 days ago













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I'm afraid we have a bit of a sadistic instructor on our hand, because I honestly did not understand this question and never found the answer in the text book, nor searching the internet.



"2. If the list is already in sorted order, how can you utilize this information in improving the linear search technique? Show your improvement in the algorithm of linear search."



As far as I know, Linear Search have only one job, which is examining each element sequentially. HOW can it be improved? What I know is the Linear Search Big-O notation is O(n) and that O(n) is the worse case performance since the element is placed the very last in the list or array. I was about to answer that question by switching to binary search but then I found that binary search would take even longer since it'll take n/2 times to locate, n/4, n/8 and do on and linear search would take as much time.



Let me know if I'm getting this question the wrong way or I'm missing something about this. Appreciate your help.










share|improve this question













I'm afraid we have a bit of a sadistic instructor on our hand, because I honestly did not understand this question and never found the answer in the text book, nor searching the internet.



"2. If the list is already in sorted order, how can you utilize this information in improving the linear search technique? Show your improvement in the algorithm of linear search."



As far as I know, Linear Search have only one job, which is examining each element sequentially. HOW can it be improved? What I know is the Linear Search Big-O notation is O(n) and that O(n) is the worse case performance since the element is placed the very last in the list or array. I was about to answer that question by switching to binary search but then I found that binary search would take even longer since it'll take n/2 times to locate, n/4, n/8 and do on and linear search would take as much time.



Let me know if I'm getting this question the wrong way or I'm missing something about this. Appreciate your help.







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









user9578589

14




14








  • 1




    You can choose a different starting point
    – leonardkraemer
    2 days ago










  • If it is a linked list then locating the middle element really needs O(n/2). For an ArrayList it would be just O(1)
    – Michael Butscher
    2 days ago










  • I did, and as I said above, Binary search would be slower for slower for sorted list.
    – user9578589
    2 days ago






  • 2




    You have to specify the data-structure that you are searching in and all other factors, otherwise trying to optimize is pointless.
    – leonardkraemer
    2 days ago










  • A math question: is the list numerical and is it bound by the range of integers? That is to say, there is a clever math approach that can be taken if the values you're sorting against are known to be numerical only (e.g. no lexicographical strings or decimals) which can be used to optimize the approach.
    – Makoto
    2 days ago














  • 1




    You can choose a different starting point
    – leonardkraemer
    2 days ago










  • If it is a linked list then locating the middle element really needs O(n/2). For an ArrayList it would be just O(1)
    – Michael Butscher
    2 days ago










  • I did, and as I said above, Binary search would be slower for slower for sorted list.
    – user9578589
    2 days ago






  • 2




    You have to specify the data-structure that you are searching in and all other factors, otherwise trying to optimize is pointless.
    – leonardkraemer
    2 days ago










  • A math question: is the list numerical and is it bound by the range of integers? That is to say, there is a clever math approach that can be taken if the values you're sorting against are known to be numerical only (e.g. no lexicographical strings or decimals) which can be used to optimize the approach.
    – Makoto
    2 days ago








1




1




You can choose a different starting point
– leonardkraemer
2 days ago




You can choose a different starting point
– leonardkraemer
2 days ago












If it is a linked list then locating the middle element really needs O(n/2). For an ArrayList it would be just O(1)
– Michael Butscher
2 days ago




If it is a linked list then locating the middle element really needs O(n/2). For an ArrayList it would be just O(1)
– Michael Butscher
2 days ago












I did, and as I said above, Binary search would be slower for slower for sorted list.
– user9578589
2 days ago




I did, and as I said above, Binary search would be slower for slower for sorted list.
– user9578589
2 days ago




2




2




You have to specify the data-structure that you are searching in and all other factors, otherwise trying to optimize is pointless.
– leonardkraemer
2 days ago




You have to specify the data-structure that you are searching in and all other factors, otherwise trying to optimize is pointless.
– leonardkraemer
2 days ago












A math question: is the list numerical and is it bound by the range of integers? That is to say, there is a clever math approach that can be taken if the values you're sorting against are known to be numerical only (e.g. no lexicographical strings or decimals) which can be used to optimize the approach.
– Makoto
2 days ago




A math question: is the list numerical and is it bound by the range of integers? That is to say, there is a clever math approach that can be taken if the values you're sorting against are known to be numerical only (e.g. no lexicographical strings or decimals) which can be used to optimize the approach.
– Makoto
2 days ago












1 Answer
1






active

oldest

votes

















up vote
3
down vote














I found that binary search would take even longer since it'll take n/2 times to locate, n/4, n/8 and do on and linear search would take as much time.




It depends on whether List::get(int) is O(1) operation or an O(N) operation. That depends on the list class.




  • For a LinkedList it is O(N). Calling get(i) has to follow the links i times starting from the head of the list.


  • For an ArrayList it is O(1). Calling get(i) is just an array[i] operation on the list's element array.



Redo your calculations for binary search with the assumption that get is O(1) and (if your analysis is correct, and your understanding of binary search is correct) you should get O(logN) for lookup by value using binary search.



(But yes, binary search on a linked list is not an optimization relative to linear search. It would be O(N^2) - an anti-optimization, given that linear search is O(N).)






I'm afraid we have a bit of a sadistic instructor on our hand.




Possibly. An alternative explanation is that the instructor is trying to encourage you to improve your analytical thinking skills by getting you to work things out for yourself. That includes figuring out your mistakes, including possibly incorrect assumptions you are making. Sometimes, the best way to learn from a fundamental mistake is "the hard way".






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%2f53402668%2fimproving-the-linear-search-technique%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
    3
    down vote














    I found that binary search would take even longer since it'll take n/2 times to locate, n/4, n/8 and do on and linear search would take as much time.




    It depends on whether List::get(int) is O(1) operation or an O(N) operation. That depends on the list class.




    • For a LinkedList it is O(N). Calling get(i) has to follow the links i times starting from the head of the list.


    • For an ArrayList it is O(1). Calling get(i) is just an array[i] operation on the list's element array.



    Redo your calculations for binary search with the assumption that get is O(1) and (if your analysis is correct, and your understanding of binary search is correct) you should get O(logN) for lookup by value using binary search.



    (But yes, binary search on a linked list is not an optimization relative to linear search. It would be O(N^2) - an anti-optimization, given that linear search is O(N).)






    I'm afraid we have a bit of a sadistic instructor on our hand.




    Possibly. An alternative explanation is that the instructor is trying to encourage you to improve your analytical thinking skills by getting you to work things out for yourself. That includes figuring out your mistakes, including possibly incorrect assumptions you are making. Sometimes, the best way to learn from a fundamental mistake is "the hard way".






    share|improve this answer



























      up vote
      3
      down vote














      I found that binary search would take even longer since it'll take n/2 times to locate, n/4, n/8 and do on and linear search would take as much time.




      It depends on whether List::get(int) is O(1) operation or an O(N) operation. That depends on the list class.




      • For a LinkedList it is O(N). Calling get(i) has to follow the links i times starting from the head of the list.


      • For an ArrayList it is O(1). Calling get(i) is just an array[i] operation on the list's element array.



      Redo your calculations for binary search with the assumption that get is O(1) and (if your analysis is correct, and your understanding of binary search is correct) you should get O(logN) for lookup by value using binary search.



      (But yes, binary search on a linked list is not an optimization relative to linear search. It would be O(N^2) - an anti-optimization, given that linear search is O(N).)






      I'm afraid we have a bit of a sadistic instructor on our hand.




      Possibly. An alternative explanation is that the instructor is trying to encourage you to improve your analytical thinking skills by getting you to work things out for yourself. That includes figuring out your mistakes, including possibly incorrect assumptions you are making. Sometimes, the best way to learn from a fundamental mistake is "the hard way".






      share|improve this answer

























        up vote
        3
        down vote










        up vote
        3
        down vote










        I found that binary search would take even longer since it'll take n/2 times to locate, n/4, n/8 and do on and linear search would take as much time.




        It depends on whether List::get(int) is O(1) operation or an O(N) operation. That depends on the list class.




        • For a LinkedList it is O(N). Calling get(i) has to follow the links i times starting from the head of the list.


        • For an ArrayList it is O(1). Calling get(i) is just an array[i] operation on the list's element array.



        Redo your calculations for binary search with the assumption that get is O(1) and (if your analysis is correct, and your understanding of binary search is correct) you should get O(logN) for lookup by value using binary search.



        (But yes, binary search on a linked list is not an optimization relative to linear search. It would be O(N^2) - an anti-optimization, given that linear search is O(N).)






        I'm afraid we have a bit of a sadistic instructor on our hand.




        Possibly. An alternative explanation is that the instructor is trying to encourage you to improve your analytical thinking skills by getting you to work things out for yourself. That includes figuring out your mistakes, including possibly incorrect assumptions you are making. Sometimes, the best way to learn from a fundamental mistake is "the hard way".






        share|improve this answer















        I found that binary search would take even longer since it'll take n/2 times to locate, n/4, n/8 and do on and linear search would take as much time.




        It depends on whether List::get(int) is O(1) operation or an O(N) operation. That depends on the list class.




        • For a LinkedList it is O(N). Calling get(i) has to follow the links i times starting from the head of the list.


        • For an ArrayList it is O(1). Calling get(i) is just an array[i] operation on the list's element array.



        Redo your calculations for binary search with the assumption that get is O(1) and (if your analysis is correct, and your understanding of binary search is correct) you should get O(logN) for lookup by value using binary search.



        (But yes, binary search on a linked list is not an optimization relative to linear search. It would be O(N^2) - an anti-optimization, given that linear search is O(N).)






        I'm afraid we have a bit of a sadistic instructor on our hand.




        Possibly. An alternative explanation is that the instructor is trying to encourage you to improve your analytical thinking skills by getting you to work things out for yourself. That includes figuring out your mistakes, including possibly incorrect assumptions you are making. Sometimes, the best way to learn from a fundamental mistake is "the hard way".







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 2 days ago









        Stephen C

        508k69554905




        508k69554905






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402668%2fimproving-the-linear-search-technique%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

            Berounka

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

            Sphinx de Gizeh