Sha256: 224c0dd4492510d23a2511283320895980d16dcebcc9da9d087c27234f51704c

Contents?: true

Size: 1.14 KB

Versions: 396

Compression:

Stored size: 1.14 KB

Contents

enum BinarySearchError: Error {
    case unsorted
}

struct BinarySearch<T: Comparable> {

    let list: [T]
    var middle: Int {
        return list.count / 2
    }

    init(_ list: [T]) throws {
        guard list == list.sorted(by: <) else {
            throw BinarySearchError.unsorted
        }
        self.list = list
    }

    func searchFor(_ datum: T) -> Int? {
        let middleItem = list[middle]

        if middleItem == datum {
            return middle
        } else if middleItem > datum {
            let sublist = Array(list[0...middle])
            guard sublist != list else {
                return nil
            }
            guard let binary = try? BinarySearch(sublist) else {
                return nil
            }
            return binary.searchFor(datum)
        } else {
            let sublist = Array(list[middle..<list.count])
            guard sublist != list else {
                return nil
            }

            guard let binary = try? BinarySearch(sublist),
                let index = binary.searchFor(datum) else {
                    return nil
            }
            return index + middle

        }
    }

}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.97 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.96 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.95 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.94 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.93 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.92 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.91 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.90 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.89 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.88 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.87 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.86 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.85 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.84 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.83 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.82 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.81 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.80 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.79 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift