vendredi 7 février 2020

Does array.count and array[0 ...< index] slow down a binary search?

today I did a test for a job and was asked to search through an array of integers, this is the question:

The goal of this exercise is to check the presence of a number in an array.

Specifications:

The items are integers arranged in ascending order.

The array can contain up to 1 million items

Implement the function existsInArray(_ numbers: [Int], _ k: Int) so that it returns true if k belongs to numbers, otherwise the function should return false.

Example:

let numbers = [-9, 14, 37, 102]
existsInArray(numbers, 102) //returns
true existsInArray(numbers, 36) //returns false

Note: Try to save CPU cycles

Alright, so I gave my answer which is the code below and waited for the result

func existsInArray(_ numbers: [Int], _ k: Int) -> Bool {
    if numbers.isEmpty {
        return false
    }
    let numbersHalfIndex: Int = (numbers.count/2)
    if k == numbers[numbersHalfIndex] {
        return true
    } else if k != numbers[0] && numbers.count == 1 {
        return false
    } else if k <= numbers[numbersHalfIndex] {
        let leftHalfNumbersArray = numbers[0 ..< numbersHalfIndex]
        return existsInArray(Array(leftHalfNumbersArray), k)
    } else if k > numbers[numbersHalfIndex] {
        let rightHalfNumbersArray = numbers[numbersHalfIndex ..< numbers.count]
        return existsInArray(Array(rightHalfNumbersArray), k)
    } else {
        return false
    }
}

So turns out that "The solution doesn't work in a reasonable time with one million items" and now I don't know what I did wrong since binary search is fast as f*ck.

My only guess is that maybe number.count or numbers[0 ...< numbersHalfIndex] or numbers[numbersHalfIndex ...< number.count] makes everything go slower than expected.

Am I tripping or something?

Aucun commentaire:

Enregistrer un commentaire