in reply to Re^2: Unexpected output for given program with write function
in thread Unexpected output for given program with write function
No $hashref2 = $VAR2->[$k]; is not deprecated, and what warning did you get?
Perl uses different operators for comparing numbers or strings. See perlop for details.
Also, I was thinking about this code in your program:
push @array, $k; # Sort the array now because we pushed the value in ar +ray. my $srt; for (my $s=0;$s<@array; $s++) { for (my $r=0;$r<@array;$r++) { if ($s ne $r) { if($array[$s]<$array[$r]) { $srt=$array[$s]; $array[$s]=$array[$r]; $array[$r]=$srt; } } } }
You are adding one element to an array and then sorting the whole array using an algorithm that is O( N2 ). And even if you switched to Perl's built-in sort function:
that would at best be O( log N ). However, you don't really need to sort the array, just insert $k in the correct place which would give you an O( N ) algorithm:@array = sort { $a <=> $b } @array, $k;
my $index = 0; ++$index while $k >= $array[ $index ]; splice @array, $index, 0, $k;
|
|---|