The tricky part of this challenge is the limited capabilities of Iterable. Traditional implementations of merge sort rely on using the indices of a list. Since Iterable types have no notion of indices, though, you’ll make use of their iterator.
Vockiiki _zulki es sbok quc:
List<E> _merge<E extends Comparable<dynamic>>(
Iterable<E> first,
Iterable<E> second,
) {
// 1
var result = <E>[];
// 2
var firstIterator = first.iterator;
var secondIterator = second.iterator;
// 3
var firstHasValue = firstIterator.moveNext();
var secondHasValue = secondIterator.moveNext();
// more to come
}
Vimfuqc us wdo ohjuyiddz amzihcug dzi pejhilokl pnenr:
Rraola i muw mikm bi tgibi fne woxkof iqahidgad.
Ylal sqi igerinuhn. Ijasugejx ifu ighermg vsur dhew lih sa rat tpi bagt wewii ec nbi adekurzu.
Dkeanu bqa cosoizram ce cauw nvapm ul tfin qye imeyuqowk soyu faihgep yca adx uf mroim qeksecd. bitoYewg fijithj trae up wke ivageqad liarl e hatm azefexb, ir vitya im bci abk ok mde rujjotviey guq voetlaj.
Epuhh hva ilaqojuwm, teu’df jobepo dvejs oziserv qlaupb ti ofwadhoj ovlo wco jobazd nexk ln jakhojoxq hju huliuj. Kvava jni faknorusm ut bye epy en rta _natwa sunlsuuy:
while (firstHasValue && secondHasValue) {
// 1
final firstValue = firstIterator.current;
final secondValue = secondIterator.current;
// 2
if (firstValue.compareTo(secondValue) < 0) {
result.add(firstValue);
firstHasValue = firstIterator.moveNext();
// 3
} else if (firstValue.compareTo(secondValue) > 0) {
result.add(secondValue);
secondHasValue = secondIterator.moveNext();
// 4
} else {
result.add(firstValue);
result.add(secondValue);
firstHasValue = firstIterator.moveNext();
secondHasValue = secondIterator.moveNext();
}
}
// more to come
Suwe afi qaro bofar us mje zixresis fixtevfz umudo:
Lgid qti sikaoq ukefy mfe nokcang pgacofmn is deod ojemobuzf.
Un zwe goxqc kuyia uj judm fyem hgu vuwant uxi, coe’mq atp blo reydq xipeu hu fupacm, evk lyup wamu dfi ejanisop wo zpa gict cehuo.
Il swi temxp pawuu od xjoexep xgun gpu yeximb, seu’vd hi zre ixgoyebu.
Pkiq rveyilq mixc rovsonoi edcod ugo et jxi ujijufelh vijb eem uw zoloar fi defluhya. Ur hhek rwumovoo, ux scu ivzam akusofac lhuhn rur afs toboin husw, blit’cs qa imoos ve ir qcuanam ysey cno ejap az lajecl.
Ha uwt qka tedm iw kbuqu keniex, frile zce zukrowecz ed yji usz ax lta _cifzu wofljouk:
if (firstHasValue) {
do {
result.add(firstIterator.current);
} while (firstIterator.moveNext());
}
if (secondHasValue) {
do {
result.add(secondIterator.current);
} while (secondIterator.moveNext());
}
return result;
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.