in reply to Fast way to check if a sort was doen

Couldn't you make a flag variable and define a sort block? Concept:
$changed = 0; my @array = sort { my $result = ($a cmp $b); $changed = 1 if $result==1; # Only set changed if $a > $b $result } @oldarray;

Replies are listed 'Best First'.
Re^2: Fast way to check if a sort was doen
by dewey (Pilgrim) on Jun 28, 2007 at 19:27 UTC
    I like this idea, but I couldn't get it to work.
    my @oldarray = (0..9); my @array = sort { my $result = ($a cmp $b); print "$result "; $result } @oldarray; print "\n@array\n";
    printed these results:
    -1 -1 -1 -1 -1 -1 1 -1 1 1 1 -1 1 -1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9
    Without knowing exactly how the sorting algorithm is implemented, I don't see how we could reliably use this kind of method.

    If OP wants to write a sorting function, they can include a flag... otherwise, this problem seems pretty solidly O(N) to me.

    ~dewey