Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- using namespace std;
- const int N = 20;
- void input(int *arr, int n);
- void print(int *arr, int n);
- void input_sort(int *arr, int n);
- bool bin_srch(int *arr, int n, int t);
- bool line_srch(int *arr, int n, int t);
- int main() {
- setlocale(LC_ALL, "rus");
- srand(time(NULL));
- int arr[N], k;
- input(arr, N);
- cout << "Исходный массив: " << endl;
- print(arr, N);
- cout << "Введите искомый элемент: ";
- cin >> k;
- if (line_srch(arr, N, k))
- cout << "Элемент найден!" << endl << endl;
- else
- cout << "Такого элемента нету." << endl << endl;
- input_sort(arr, N);
- cout << "Отсортированный массив: " << endl;
- print(arr, N);
- cout << "Введите искомый элемент: ";
- cin >> k;
- if (bin_srch(arr, N, k))
- cout << "Элемент найден!" << endl << endl;
- else
- cout << "Такого элемента нету." << endl << endl;
- return 0;
- }
- void input(int *arr, int n) {
- for (int i = 0; i < n; i++) {
- arr[i] = rand() % 101 - 50;
- }
- }
- void print(int *arr, int n) {
- for (int i = 0; i < n; i++) {
- cout << arr[i] << ' ';
- }
- cout << '\n';
- }
- void input_sort(int *arr, int n) {
- for (int i = 1; i < n; i++) {
- int cur = i;
- while (arr[cur - 1] > arr[cur] && cur > 0) {
- swap(arr[cur - 1], arr[cur]);
- cur--;
- }
- }
- }
- bool bin_srch(int *arr, int n, int t) {
- int l = 0, r = n - 1, m;
- while (l < r) {
- m = (l + r) / 2;
- t > arr[m] ? l = m + 1 : r = m;
- }
- if (t == arr[l]) return true;
- return false;
- }
- bool line_srch(int *arr, int n, int t) {
- for (int i = 0; i < n; i++) {
- if (arr[i] == t) return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement