Skip to content

Instantly share code, notes, and snippets.

@ordazgustavo
Created December 20, 2019 04:05
Show Gist options
  • Save ordazgustavo/b66ce98b486f2948580d2ebcfd5addea to your computer and use it in GitHub Desktop.
Save ordazgustavo/b66ce98b486f2948580d2ebcfd5addea to your computer and use it in GitHub Desktop.
An example of binary search written in Swift
let nums = [1, 2, 5, 13, 25, 50, 75, 100]
let names = ["Angel", "Bianca", "Carlos", "Gustavo", "Maria", "Samuel", "Victor"]
struct Date {
let year: Int
let month: Int
let day: Int
}
extension Date: Comparable {
static func < (lhs: Date, rhs: Date) -> Bool {
if lhs.year != rhs.year {
return lhs.year < rhs.year
} else if lhs.month != rhs.month {
return lhs.month < rhs.month
}
return lhs.day < rhs.day
}
static func == (lhs: Date, rhs: Date) -> Bool {
lhs.year == rhs.year && lhs.month == rhs.month && lhs.day == rhs.day
}
}
// TOOL album release dates
let dates = [
Date(year: 1993, month: 4, day: 6),
Date(year: 1996, month: 10, day: 1),
Date(year: 2001, month: 5, day: 15),
Date(year: 2006, month: 5, day: 2),
Date(year: 2019, month: 8, day: 30)
]
/// A binary search recursive function that returns a boolean indicating if the element is in the array.
///
/// This function is generic so you can pass any kind of array as long as its elements conform to
/// `Comparable`.
///
/// - Parameters:
/// - array: The array of elements to search
/// - x: The element to search for
/// - left: The array start index
/// - right: The array end index
func binarySearchRecursive<T: Comparable>(
array: [T],
x: T,
left: Int,
right: Int
) -> Bool {
if left > right {
return false
}
let mid = (left + right) / 2
if array[mid] == x {
return true
} else if x < array[mid] {
return binarySearchRecursive(
array: array,
x: x,
left: left,
right: mid - 1
)
}
return binarySearchRecursive(
array: array,
x: x,
left: mid + 1,
right: right
)
}
/// A binary search recursive function that returns the `index` of the element in the array, `nil` otherwise.
///
/// This function is generic so you can pass any kind of array as long as its elements conform to
/// `Comparable`.
///
/// - Parameters:
/// - array: The array of elements to search
/// - x: The element to search for
/// - left: The array start index
/// - right: The array end index
func binarySearchRecursive<T: Comparable>(
array: [T],
x: T,
left: Int,
right: Int
) -> Int? {
if left > right {
return nil
}
let mid = (left + right) / 2
if array[mid] == x {
return mid
} else if x < array[mid] {
return binarySearchRecursive(
array: array,
x: x,
left: left,
right: mid - 1
)
}
return binarySearchRecursive(
array: array,
x: x,
left: mid + 1,
right: right
)
}
/// A wrapper on the `binarySearchRecursive` function.
///
/// - Parameters:
/// - x: The element to search for
/// - array: The array in which to search the element.
func find<T: Comparable>(element x: T, in array: [T]) -> Bool {
binarySearchRecursive(array: array, x: x, left: 0, right: array.count - 1)
}
/// A wrapper on the `binarySearchRecursive` function.
///
/// - Parameters:
/// - x: The element to search for.
/// - array: The array in which to search the element.
func findIndex<T: Comparable>(of x: T, in array: [T]) -> Int? {
binarySearchRecursive(array: array, x: x, left: 0, right: array.count - 1)
}
find(element: 13, in: nums)
find(element: "Samuel", in: names)
// An element that doesn't exist in the array
find(element: "David", in: names)
find(element: Date(year: 2006, month: 5, day: 2), in: dates)
findIndex(of: 13, in: nums)
findIndex(of: "Samuel", in: names)
// An element that doesn't exist in the array
findIndex(of: "David", in: names)
findIndex(of: Date(year: 2006, month: 5, day: 2), in: dates)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment