Hameed has asked for the wisdom of the Perl Monks concerning the following question:
Why do you think someone would call the following a 'naive' solution?
Create sublists from an array of integers ,in increasing order:
Input: [ 1,2,3,4,5,6,7,8,3,5,6,3,8,1,7,8 ]my @array1 = (1,2,3,4,5,6,7,8,3,5,6,3,8,1,7,8); my @newarray; my @tmparray; for (0..$#array1) { if ($_> 0 && $array1[$_] < $array1[$_-1]) { push (@newarray, [@tmparray]); undef(@tmparray); } push (@tmparray, $array1[$_]); } push (@newarray, [@tmparray]) if defined @tmparray;
Then from the output generated sort items in a single array:
My assumption was that I no longer had @array1 to work with and had to create the final sorted array from @newarray.
Also, is this wrong to say in this particular case the complexity is O(n) disregarding the complexity of sort?my @array3; for (@newarray) { push @array3, @$_; } @array3 = sort @array3;
FYI: This was in an interview...
|
|---|