Re: Sorting Arrays Without using SORT function
by Limbic~Region (Chancellor) on Feb 15, 2011 at 20:42 UTC
|
PerlCool,
As you can probably tell, we believe your question to be homework which is very much frowned on if not acknowledged as such (and sometimes even then). It is perfectly possible that you want to know how sorting algorithms work and can't understand perl's source code written in C nor the myriad of explanations online. If that is the case, then make sure you say that.
Learning the pros and cons of merge sort in comparison to a bubble sort is worth pursuing in my opinion. For that reason I am going to give you the tools to write your own bubble sort and that should give you enough knowledge to explore more advanced sorting algorithms. If you get stuck and need help, make sure you are much more forthcoming with what you are doing and why (because I too think this is homework).
The cmp operator will compare two strings and tell you if they are equal or not. If they are not equal, it will tell you which one is greater than the other ASCIIbetically. You can swap the value of two array elements using the following syntax: ($list[0], $list[1]) = ($list[1], $list[0]); Now consider you are compare the first two elements of the list and they are out of order - just swap them. Next, compare the second element, if they are order, swap them. Repeat this process until you reach the end of the list. Start back at the beginning and continue until you go all the way through the list without swapping any elements - then you know it is in sorted order.
| [reply] [d/l] |
Re: Sorting Arrays Without using SORT function
by TomDLux (Vicar) on Feb 15, 2011 at 17:18 UTC
|
- Write out the values on 3x5 index cards.
- Arrange the index cards into sorted order.
- Type the values into the computer.
- ...
- Profit!!!
I think you need to be specific on what you mean by sorting without sorting. Ask your teacher or TA for additional guidance.
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
| [reply] |
Re: Sorting Arrays Without using SORT function
by Fletch (Bishop) on Feb 15, 2011 at 17:24 UTC
|
Sure, but runtime depends on your luck.
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw( shuffle reduce );
my @list = qw( f a d e z x c b );
sub in_order {
my $state = 1;
our( $a, $b );
my $ordered = reduce { if( $state and $b lt $a ) { $state = undef; }
+ $b } @_;
return $state;
}
my $shuffles = 0;
my @ordered = @list;
my %memory;
until ( in_order( @ordered ) ) {
$memory{ join("\0",@ordered) } = 1;
while( 1 ) {
@ordered = shuffle( @list );
$shuffles++;
last unless $memory{ join("\0",@ordered) };
}
}
print "took $shuffles shuffles to get\n\t", join( "\n\t", @ordered ),
+"\n";
exit 0;
__END__
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] |
|
|
use strict;
use warnings;
my @array = qw(h o m e w o r k);
while (not isSorted(@array))
{
print +(join ", ", @array) . "\r";
my ($x, $y) = (rand(scalar @array), rand(scalar @array));
($array[$x], $array[$y]) = ($array[$y], $array[$x]);
}
print "\nSort complete!\n";
print join ", ", @array;
sub isSorted
{
my $x = shift;
while (my $y = shift)
{
return 0 unless ($x cmp $y) < 1;
$x = $y;
}
return 1;
}
| [reply] [d/l] |
Re: Sorting Arrays Without using SORT function
by chrestomanci (Priest) on Feb 15, 2011 at 17:10 UTC
|
This smells like a homework question, in other words PerlCool has been asked to write a sort function.
The only hint I will give, is to point out the cmp operator which returns -1, 0, or +1 to compare to scalars, and the spaceship operator <=> which does the same with numbers.
| [reply] [d/l] [select] |
Re: Sorting Arrays Without using SORT function
by FunkyMonk (Bishop) on Feb 15, 2011 at 17:32 UTC
|
| [reply] |
Re: Sorting Arrays Without using SORT function
by kennethk (Abbot) on Feb 15, 2011 at 16:56 UTC
|
| [reply] |
Re: Sorting Arrays Without using SORT function
by ikegami (Patriarch) on Feb 15, 2011 at 18:53 UTC
|
use Algorithm::Loops qw( NextPermute );
sub mysort {
my @a = @_;
1 while NextPermute(@a);
return @a;
}
my @unsorted = qw( a c b d );
my @sorted = mysort(@unsorted);
print("@sorted\n");
use IPC::Run3 qw( run3 );
sub mysort {
my @unsorted = map "$_\n", @_;
run3([ 'sort' ], [ map "$_\n", @unsorted ], \my @sorted);
chomp(@sorted);
return @sorted;
}
my @unsorted = qw( a c b d );
my @sorted = mysort(@unsorted);
print("@sorted\n");
| [reply] [d/l] [select] |
Re: Sorting Arrays Without using SORT function
by Khen1950fx (Canon) on Feb 15, 2011 at 19:02 UTC
|
Actually, you do want to use sort. It's exponentially faster when used by itself. I think that you just need a quick way to do your array. Try this:
#!/usr/bin/perl
use strict;
use warnings;
my @array = qw( one.txt 1.txt two.log 2.log);
@array = sort( @array );
print "@array\n";
sub sort {
sort { $a cmp $b unless $a <=> $b; } @_;
}
| [reply] [d/l] |
Re: Sorting Arrays Without using SORT function
by locked_user sundialsvc4 (Abbot) on Feb 15, 2011 at 18:26 UTC
|
Ask your teacher to give you the answer.
Or, just withdraw from the class now to save yourself the ignominy of flunking out, and also to save your teacher the time of further dealing with you.
| |