forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearch.swift
28 lines (24 loc) · 885 Bytes
/
binarySearch.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
func binarySearch(array: [Int], searchedElement: Int) -> Int {
var firstIndex: Int = 0
var lastIndex: Int = array.count - 1
var middleIndex: Int;
while firstIndex <= lastIndex {
middleIndex = (firstIndex + lastIndex) / 2
if (searchedElement == array[middleIndex]) {
return middleIndex
}
searchedElement < array[middleIndex] ? (lastIndex = middleIndex - 1) : (firstIndex = middleIndex + 1)
}
return -1 // If the searched element isn't in the array
}
func main() {
let sortedArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let elementToSearch = 5
let result = binarySearch(array: sortedArray, searchedElement: elementToSearch)
if result != -1 {
print("Element \(elementToSearch) found at index \(result)")
} else {
print("Element \(elementToSearch) not found in the array.")
}
}
main()