Thread Liste von Arrays sortieren (14 answers)
Opened by scriptor at 2018-04-12 08:54

rosti
 2018-04-13 11:35
#188272 #188272
User since
2011-03-19
3194 Artikel
BenutzerIn
[Homepage]
user image
Kommt darauf an, wie man den Helper einsetzt, z.B. so:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
my $cnt_sort = 1;
my $cnt_sort_schwartz = 1;


my @array = (
    [25,1],
    [3,7],
    [1,2],
    [2,4],
    [2,2],
    [35,34]
);

# Native
my @dummy = sort{ helper($a) <=> helper($b) }@array ;

# nach Schwartz
my @dummy_schwartz = 
    map{$_->[0]}
    sort{ $a->[1] <=> $b->[1] }
    map{[$_, helper_schwartz($_)]} @array;


print Dumper \@dummy_schwartz;


print "Anzahl der Aufrufe cnt_sort: $cnt_sort\n";
print "Anzahl der Aufrufe cnt_sort_schwartz: $cnt_sort_schwartz\n";


sub helper{
    my $ref = shift;
    $cnt_sort++;
    return abs($ref->[1] - $ref->[0]); 
}

sub helper_schwartz{
    my $ref = shift;
    $cnt_sort_schwartz++;
    return abs($ref->[1] - $ref->[0]); 
}


Anzahl der Aufrufe cnt_sort: 17
Anzahl der Aufrufe cnt_sort_schwartz: 7

View full thread Liste von Arrays sortieren