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.139 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.138 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.137 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.136 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.135 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.134 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.133 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.132 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.131 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.130 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.129 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.128 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.127 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.126 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.125 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.124 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.123 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.122 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.121 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.120 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift