-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.cpp
More file actions
41 lines (33 loc) · 953 Bytes
/
Copy pathsimple.cpp
File metadata and controls
41 lines (33 loc) · 953 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
/*
## Простой поиск
* На вход подаётся отсортированный массив и заданное число
* Возвращается индекс или -1, если не найдено
* Скорость: O(n), память: O(1)
*/
int simple_search(vector<int> nums, int query) {
for (int i = 0; i < size(nums); ++i) {
if (nums[i] == query) {
return i;
}
}
return -1;
}
int main() {
// Проверка работы алгоритма вводом
int n; // Кол-во чисел в массиве
scanf("%d\n", &n);
vector<int> nums; // Отсортированный массив
for (int i = 0; i < n; i++) {
int temp;
scanf("%d ", &temp);
nums.push_back(temp);
}
int query;
scanf("%d", &query);
printf("\nResult: %d", simple_search(nums, query));
return 0;
}