Simple binary search methods for arrays.
With SPM, add the following to the dependencies of your Package.swift
.package(url: "https://github.com/ladislas/SwiftBinarySearch", from: "1.0.0")
Then in your code place at the top of the files where you need Swift Events:
import SwiftBinarySearch
See source code and tests for more documentation and more examples.
let myArray = [0, 1, 2, 4]
let index = binarySearch(for: 3, in: myArray)
print(index) // --> "3"
// or
let myArray = [0, 1, 2, 4]
binarySearchAndInsert(for: 3, in: myArray)
print(myArray.description) // --> "[0, 1, 2, 3, 4]"
let myArray = [0, 1, 2, 4]
let index = myArray.binarySearch(for: 3)
print(index) // --> "3"
// or
var myArray = [0, 1, 2, 4] // declare array as var
myArray.binarySearchAndInsertInplace(element: 3)
print(myArray.description) // --> "[0, 1, 2, 3, 4]"
Made with ❤️ by:
- Ladislas de Toldi - ladislas
MIT/Apache-2.0 @ Ladislas de Toldi