Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Insert Into a Sorted Array

by ketema (Scribe)
on Sep 29, 2004 at 03:20 UTC ( [id://394843]=note: print w/replies, xml ) Need Help??


in reply to Re: Insert Into a Sorted Array
in thread Insert Into a Sorted Array

This works, but is it O(N)? Sort algorithms, man I think I remember this, can't I do a binary search? Would that be faster to find the correct placement, or am I getting terms mixed up? Thank you

Replies are listed 'Best First'.
Re^3: Insert Into a Sorted Array
by ikegami (Patriarch) on Sep 29, 2004 at 03:22 UTC
    Here's your bin search. Worst case time for the search (not counting the splicing): O(log N)
    sub compare { $a cmp $b } sub binsearch { # Returns the index of the position before # the one where $s would be found, when $s # is not found. my ($f, $s, $list) = @_; my $i = 0; my $j = $#$list; my $k; my $c; $a = $s; for (;;) { $k = int(($j-$i)/2) + $i; $b = $list->[$k]; $c = &$f(); return $k if ($c == 0); if ($c < 0) { $j = $k-1; return $j if ($i > $j); } else { $i = $k+1; return $k if ($i > $j); } } } @a = qw( z b x c y a ); @b = qw( o m n ); @a = sort compare @a; @b = sort compare @b; splice(@a, binsearch(\&compare, $b[0], \@a)+1, 0, @b); print(@a); # abcmnoxyz

    Of course, in your case, it will be
    splice(@known, binsearch(\&byIndex, $newValue, \@known)+1, 0, $newValue);

    Update: Passing array to binsearch using a reference now. oops!

      Passing @a not \@a means that perl copies the entire array to the function for no reason, and thus loses a lot of the potential efficiency.

      This is the fastest, especially on a 10,000 record file.
      Oh I wanted to give thanks to all of you who helped me earlier on Figuring out how to embed c into PERL. Still not perfect at it, but i got it to work for what I need. Thanks Again to all you Wise monks. Ketema
Re^3: Insert Into a Sorted Array
by tachyon (Chancellor) on Sep 29, 2004 at 03:31 UTC

    Yes you can do a binary search. Yet is will find the correct placement faster for large lists. In fact I have a module on cpan called File::SortedSeek that implements binary searches in large files so I appreciate the algorithm :-) However in this scenario having found the index you then have a practical problem. It is not possible to insert a value into the middle of an array, which is really just a contiguous sequence of SV* What you have to do is make space for it. Splice does that by moving a large chunk (or the entire array). The bubble algorithm also does it and is (in past testing) about twice as fast as just calling sort. Here is a binary search implementation. Benchmarking the options is left to you.

    my @ary = (1..4, 6..10); for my $val( 0, 5, 11, 0, 5, 11 ) { binary( \@ary, $val ); print "@ary\n"; } sub binary { my ( $ary, $val ) = @_; my ( $min, $max, $last, $i ) = ( 0, scalar @{$ary}, 0, 0 ); while ( 1 ) { $i = int( ($min+$max)/2 ); # print "i=$i\n"; last if $last == $i; $last = $i; if ( $ary->[$i] < $val ) { $min = $i; } elsif ( $ary->[$i] > $val ) { $max = $i; } else { # values are equal so we have a valid index last; } } $i++ if $i; # for index 0 we want that, otherwise we want next p +osition splice @$ary, $i, 0, $val; } __DATA__ 0 1 2 3 4 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 11 0 0 1 2 3 4 5 6 7 8 9 10 11 0 0 1 2 3 4 5 5 6 7 8 9 10 11 0 0 1 2 3 4 5 5 6 7 8 9 10 11 11

    cheers

    tachyon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://394843]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found