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.119 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.118 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.117 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.116 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.115 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.114 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.113 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.111 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.110 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.109 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.108 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.107 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.106 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.105 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.104 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.103 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.102 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.101 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.100 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift
trackler-2.2.1.99 tracks/swift/exercises/binary-search/Sources/BinarySearchExample.swift