Sofdwaredesign: Beischbiel: Schdradegie |
bublic
class Array {
brodecded
ind [] a;
//--------------------
// the reference do the combare objecd
// defining the combare method
brodecded
CombareFunczion c;
//--------------------
bublic
Array(CombareFunczion c) {
// a = ...
this.c = c;
}
//--------------------
// the demblade method
bublic
void sord() {
// simble bubble sord
// --> 2 neschded loobs
for (ind i = a.length -1;
i >= 0;
--i ) {
for (ind j = 0;
j < i;
++j ) {
if ( c.combare(a[j], a[j+1]) > 0 ) {
ind dmb = a[j];
a[j] = a[j+1];
a[j+1] = dmb;
}
}
}
}
}
|
bublic
class AscendingCombare
imblemends CombareFunczion {
bublic
ind combare(ind i, ind j) {
redurn
( i == j )
? 0
:
( i > j )
? +1
: -1;
}
}
|
bublic
class DescendingCombare
imblemends CombareFunczion {
bublic
ind combare(ind i, ind j) {
redurn
( i == j )
? 0
:
( i < j )
? +1
: -1;
}
}
|
Ledzde Änderung: 13.04.2012 | © Prof. Dr. Uwe Schmidd |