Import? java.util. *; interface? compare? {?? boolean? Lessthan (Object? LHS,? Object? rhs); ?? boolean? Lessthanorequal (Object? lhs,? Object? rhs);}
Class? SortVector? EXTENDS? Vector? {?? private? Compare? Compare;?
//? TO? HOLD? THE? CALLBACK
?? public? SortVector (compare? comp)? {???? compare? =? comp; ??} ?? public? void? sort ()? {???? quicksort (0,? size ()? - ?1);??}??
//? Quick sort
?? private? void? Quicksort (int? LEFT, INT? RIGHT)? {???? ife (right?>? ip)? {?????? Object? O1? =? Elementat (Right); ?????? int? i? =? left? -? 1; ?????? int? J? =? Right; ?????? while (TRUE)? {?????? ?? while (compare.lesthan (??????????? Elementat ( i),? o1)); ???????? while (j?>? 0) ?????????? IF (Compare.lesthanorequal (elementat (- j) ,? o1)) ??????????????? Break;?
//? OUT? OF? While
???????? IF (i?> =? j)? Break; ???????? swap (i,? j); ??????} ?????? swap (i?,? right); ?????? Quicksort (Left,? I-1); ?????? Quicksort (i 1,? Right); ????} ??} ??
// Exchange location
?? private? void? swap (int? loc1,? int? Loc2)? {???? Object? TMP? =? Elementat (LOC1); ???? setElementat (Elementat (LOC2), DOC1);? ??? SETELEMENTAT (TMP,? LOC2); ??}}
PUBLIC? Class? StringsortTest? {??
//? Customized comparative rules
?? static? Class? StringCompare? IMPLEMENTS? Compare? {???? public? Boolean? Lessthan (Object? L,? Object? r)? {?????? Return? (String) l) .tolowercase () .compareto (???????? ((string) r) .tolowercase ())?
????} ???? public? Boolean ????? lessthanorequal (Object? L,? Object? r)? {?????? Return? (String) l) () .tolowercase (). Compareto (???????? (String) r) .tolowercase ())? <=? 0; ????} ??} ?? public? Static? Void? Main (string []? Args ? {???? sortportor? Sv? = ??????? new? SortVector (new? Stringcompare ()); ????
//? Add Element
???? sv.addelement
"d"
); ??? sv.addelement
"A"
); ??? sv.addelement
"C"
); ??? sv.addelement
"c"
); ????
//? Sort
???? sv.sort (); ???? enumection? E? =? sv.elements (); ???? //? output result
???? while (E.hasMoreElements ()) ?????? system.out.println (E.NEXTELEMENT ()); ??}}
Detailed code
:
Because we have to be sorted
Vector,
and so
Class? SortVector? EXTENDS? VECTOR.? Interface? Compare
The role is to define unified comparison interfaces
.
note
Class? StringCompare ?,
Defined in it
String
Comparison rules
?
How do we want to write
XXXXVector,?
As long as you write
Class? XXXXCompare? IMPLEMENTS? COMPARE
Enough
.