Hi all.

Two of the most common search algorithms are the:

linear search and the binary search

Each has it's advantages as well as it's 'faults'. In a linear search, each element of the data structure is examined. Needless to say, in a small array ( for example ) this isn't a problem since there are only a relatively small number of elements to analyze. If the array is large, a binary search would be most effective. In a binary search, the array needs to be sorted before a search is initiated. Once that has been accomplished, there exists a tremendous gain in performance ( please see below ). By establishing a 'mid-point' in the array, we are able to eliminate 1/2 of the array ( the lower or upper portion ) when comparing our key ( the value we want to find ) to it's position in the array and it's relation to the mid-point. With each successive comparison, we divide the array in 1/2 thereby establishing a new mid-point. Then it's a simple matter of establishing whether or not our key is higher or lower than it.

An array with 2_048 elements could be successfully searched using ( at most ) only 11 comparisons ( 2**11 == 2048 ). When compared to the time it would probably take to search the same array using a linear search, the benefits should be readily apparent. The formula for determining the maximum number of comparisons needed is the first power of 2 greater than the number of elements in the array.

#!/usr/bin/perl -w use strict; my @array; my $low = 0; my $mid; my $found_key = 0; my $num; my $key; my $index; @array = ( 5, 10, 30, 4, -3, 18, 101, 2001, 46, 23 ); @array = sort { $a <=> $b } @array; my $high = $#array; print "Please enter key to search for: "; chomp( $key = <STDIN> ); while( ( $low <= $high ) && !$found_key ) { $mid = ( $low + $high ) / 2; if( $key == $array[$mid] ) { $found_key = 1; $index = int( $mid ); } elsif( $key < $array[$mid] ) { $high = $mid - 1; } else { $low = $mid + 1; } } if( $found_key ) { print "$key is at position: $index\n"; } else { print "Sorry. I could not find: $key"; }


This is my first attempt at implementing a binary search in perl. If you could help me improve the code, I would be most appreciative.

Thanks,
-Katie.

In reply to Binary searching with Perl. by DigitalKitty

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.