Skip to content

Commit 94be6d1

Browse files
authored
Update binarySearch.swift
1 parent 65d4385 commit 94be6d1

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/swift/binarySearch.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,18 @@ func binarySearch(array: [Int], searchedElement: Int) -> Int {
1818
}
1919
return -1 // If the searched element isn't in the array
2020
}
21+
22+
func main() {
23+
let sortedArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]
24+
let elementToSearch = 5
25+
26+
let result = binarySearch(array: sortedArray, searchedElement: elementToSearch)
27+
28+
if result != -1 {
29+
print("Element \(elementToSearch) found at index \(result)")
30+
} else {
31+
print("Element \(elementToSearch) not found in the array.")
32+
}
33+
}
34+
35+
main()

0 commit comments

Comments
 (0)