I would like to get more into algorithms, so I need your tips and guidance. Please feel free to recommend books, courses, and websites to improve my competitive programming.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 170 |
2 | Um_nik | 162 |
3 | maomao90 | 161 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
5 | -is-this-fft- | 158 |
7 | adamant | 154 |
7 | Dominater069 | 154 |
9 | awoo | 152 |
9 | luogu_official | 152 |
I would like to get more into algorithms, so I need your tips and guidance. Please feel free to recommend books, courses, and websites to improve my competitive programming.
Binary search logic:
Suppose there is a set of numbers of 100 count. And I want to guess a number I'd start right in the middle that'd be 50. Then, if the guess is too low I'd take the remaining 50 and divide it by two and add it to the 50. We get 75. Too high ? Lower it using the same procedure. It'll be 63. Too high? Next, we do the same procedure and get 57. We'll go like this till we guess right.
To count max numbers of trials we take n=100 and use logarithms. The answer will be log100 (base2) = 7 (6.64)
in python, it would look like this:
def binary_search(list, item):
low = 0 high = len(list)-1 while low <= high: mid = (low + high) guess = list[mid] if guess == item: return mid if guess > item: high = mid - 1 else: low = mid + 1 return None
my_list = [1, 3, 5, 7, 9]
print(binary_search(my_list, 3)) # => 1
print(binary_search(my_list, -1)) # => None
Name |
---|