Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hi akvats01,

Did you try printing out the sorted list to make sure it got sorted correctly?

I did, and got:

print "Sorted => @num_list\n"; # Output: Sorted => 34 2 65 345 987 23 12 45 62 100

So the sort isn't working. Now that I know that's a problem, I look at your sort line more carefully:

@num_list= sort {a <=> b} @num_list1;
And it's obvious what's wrong; you should use "$a" and "$b". You would have found that if you used the "strict" and "warnings" pragmas. Try running this partial program and you'll see:
#!/usr/bin/perl use strict; use warnings; my @num_list1=(34,2,65,345,987,23,12,45,62,100); my @num_list= sort {a <=> b} @num_list1; print "Sorted: @num_list\n"; # Output: # # Bareword "a" not allowed while "strict subs" in use at 1026159.pl li +ne 7. # Bareword "b" not allowed while "strict subs" in use at 1026159.pl li +ne 7. # Execution of 1026159.pl aborted due to compilation errors.
The other (potential) problem I can see is that you are not doing integer math, so when you take the average of $low and $high with:
$mid = ($low+$high)/2;
you're sometimes getting a fractional result. You may need to use int to fix that.

Try those fixes to start with and see if you can get farther. One other recommendation would be to put print statements at various locations in your code to make sure you're getting the expected results each time through the loop.

say  substr+lc crypt(qw $i3 SI$),4,5

In reply to Re^3: Binary search by golux
in thread Binary search by akvats01

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (16)
    As of 2014-09-17 13:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (80 votes), past polls