Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^2: Binary search

by akvats01 (Initiate)
on Mar 29, 2013 at 17:04 UTC ( #1026173=note: print w/replies, xml ) Need Help??

in reply to Re: Binary search
in thread Binary search

Thanks to everyone. I have sorted the array as follows

@num_list1=(34,2,65,345,987,23,12,45,62,100); @num_list= sort {a <=> b} @num_list1;
Still the same results are shown. I am due to go through the algorithm implementation mentioned above. Meanwhile, I will be thankful if some light may be thrown on why the code is working partially? what precautions should I take to avoid this situation when code is working partially. Thanks.

My apologies for code not being readable. The original code, after implementing sort is :

#!/usr/bin/perl @num_list1=(34,2,65,345,987,23,12,45,62,100); @num_list= sort {a <=> b} @num_list1; $low=0; $found_key=0; $index; $high=$#num_list; print "enter the key: \t"; chomp($key=<STDIN>); while($high>=$low && !$found_key) { $mid=($low+$high)/2; if ($key == @num_list[$mid]) { $found_key=1; $index = int($mid); } if ($key > @num_list[$mid]) { $low = $mid+1; } if ($key < @num_list[$mid]) { $high = $mid-1; } } if ($found_key) { print " The key is $index\n"; } else {print "key is not found";}

Replies are listed 'Best First'.
Re^3: Binary search
by golux (Hermit) on Mar 29, 2013 at 17:39 UTC
    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 li +ne 7. # Bareword "b" not allowed while "strict subs" in use at li +ne 7. # Execution of 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
Re^3: Binary search
by farang (Chaplain) on Mar 29, 2013 at 17:41 UTC

    This line will produce a non-integer value with an odd number of elements, which can't be an index of an array.

    If you use the following at the top of your scripts and make the necessary corrections for it to compile, it will make your life easier and your code better.
    use strict; use warnings;

    Update: I see now that an array can be indexed with a non-integer value, effectively truncating the fractional part. I'm surprised that gives no warning.

    use strict; use warnings; use feature 'say'; my @list = 0..9; say $list[2.5]; say $list[2.01]; say $list[2.99]; # all these print '2'
    golux is correct of course about the sort, which is something that enabling 'strict' and 'warnings' would pick up.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026173]
[Jenda]: Sorry, 29 questions on stackoverflow regarding Perl6 this month. What a lively community.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-04-24 21:15 GMT
Find Nodes?
    Voting Booth?