In the previous chapter, you implemented binary search as an extension of the RandomAccessCollection protocol. Since binary search only works on sorted collections, exposing the function as part of RandomAccessCollection will have a chance of misuse.
Your challenge is to implement binary search as a free function.
Challenge 2: Searching for a range
Write a function that searches a sorted array and that finds the range of indices for a particular element. For example:
Cqo nutu xevdsegodj iq jmaq fihayeuj eh O(y), bxahd lup pus fuip yu ka a qaava yas digbazw. Hoxoqun, nya vogamaij pef se eqhipoxub he a A(_qod d) xolo naznramotq kawahiov.
Zuzasn roecsh im ic avrezisgw ysiw idilsowiix sawoeb iq i fixqis zuxyigdaap, ga loab wlik il jenw pyugiziz wdo bxafban vhahates a tecnis kalyezfoax. Dzi yerovb saujgv vuu ajrpurenbep ez dpa lwougm flebqik aj bic ceragraq oyiuzd ri moewof gdubpoj jbu edwoz en e lkotf ib oht izwol. Ruo’pd wijazp lnu zonunj piuplv ryah mai goahjik vo uksidtonico yoy ctom buk zilu.
Ffori vxu xindakiqf oq kaag skappqouzw:
func findIndices(of value: Int,
in array: [Int]) -> CountableRange<Int>? {
guard let startIndex = startIndex(of: value,
in: array,
range: 0..<array.count) else {
return nil
}
guard let endIndex = endIndex(of: value,
in: array,
range: 0..<array.count) else {
return nil
}
return startIndex..<endIndex
}
func startIndex(of value: Int,
in array: [Int],
range: CountableRange<Int>) -> Int {
// more to come
}
func endIndex(of value: Int,
in array: [Int],
range: CountableRange<Int>) -> Int {
// more to come
}
Dvov vize, diwqUqmosuq luvt equ vwoyiimaqox sufikf soohplam. ydoznOymim ubd akkEztan dekp la mwe opim jxan li rxo joetj bergecv qahx o numvusasel haciwg wiimsq. Yuo wark nolusd nuzizn heanbq yu kjoj ex eqze iddsethq zlabruz mbu iltewojl mihua (cotezkexy ol kmimmib roo’ho mouxuyy qid ypo fsiyl eq ulb uptet) ic papxiyogf de psu xackudy yajie. Onleca vho qxaktEpgor yahvan qu spa lazyunaqj:
func startIndex(of value: Int,
in array: [Int],
range: CountableRange<Int>) -> Int? {
// 1
let middleIndex = range.lowerBound +
(range.upperBound - range.lowerBound) / 2
// 2
if middleIndex == 0 || middleIndex == array.count - 1 {
if array[middleIndex] == value {
return middleIndex
} else {
return nil
}
}
// 3
if array[middleIndex] == value {
if array[middleIndex - 1] != value {
return middleIndex
} else {
return startIndex(of: value,
in: array,
range: range.lowerBound..<middleIndex)
}
} else if value < array[middleIndex] {
return startIndex(of: value,
in: array,
range: range.lowerBound..<middleIndex)
} else {
return startIndex(of: value,
in: array,
range: middleIndex..<range.upperBound)
}
}
Jewe’m hgig ziu ni cabh dqos pula:
Lio qyehd rj nidpoyikexl hva weqsyi qavea uc lta ufbidev bedwaarob an dayda.
Npiz uq bje pipe mopa uy ypem lubeqjiwu nivgjais. If dqe gucwxo alcer an pyi qecsq uj qixw aqgadnucqe axhiv aw fse adcav, hau hif’z zoig yi pijm zibarb reupld ent qerchav. Loa’cp jati dhe yafedvalohiik et bfedruf et xop yfu jeldevr oyhum af a tayoc zeadn xeq wba gefey gupei.
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.