-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fce3fc
commit 1f9061f
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import UIKit | ||
|
||
/// The below function returns multiple result: 'found' Result & 'indexValue' of Result | ||
/// when searched in an array using Binary Search | ||
|
||
func BinarySearch(valueToSearch: Int) -> (Bool, Int?) { | ||
|
||
let numbers = [1, 5, 15, 17, 19, 22, 24, 31, 105, 150] | ||
|
||
var left = 0 | ||
var right = numbers.count - 1 | ||
var middle = 0 | ||
var found = false | ||
var index: Int? | ||
|
||
while left <= right { | ||
middle = (left + right) / 2 | ||
|
||
if numbers[middle] == valueToSearch { | ||
found = true | ||
index = middle | ||
break | ||
} | ||
else if numbers[middle] < valueToSearch { | ||
left = middle + 1 | ||
} | ||
else { | ||
right = middle - 1 | ||
} | ||
} | ||
return (found, index) | ||
} | ||
|
||
print(BinarySearch(valueToSearch: 31)) // (true, Optional(7)) | ||
print(BinarySearch(valueToSearch: 99)) // (false, nil) | ||
print(BinarySearch(valueToSearch: 13)) // (false, nil) | ||
print(BinarySearch(valueToSearch: 17)) // (true, Optional(3)) |