Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <conio.h>
- using namespace std;
- void generator(int* mass, int n);
- void print(int* mass, int n);
- int linSearch(int* mass, int y, int z);
- void BubbleSort(int* mass, int n);
- int BinSearch(int* mass, int n, int k);
- int main() {
- srand(time(NULL));
- setlocale(LC_ALL, "Rus");
- const int n = 20;
- int A[n], x, c, nElement = 0;
- generator(A, n);
- print(A, n);
- cout << "Искомое число: ";
- cin >> x;
- nElement = linSearch(A, n, x);
- if (nElement != -1) cout << "Число " << x << " находится в ячейке с индексом: " << nElement << endl;
- else cout << "В массиве нет такого значения" << endl;
- BubbleSort(A, n);
- cout << "\nОтсортированный массив:" << endl;
- print(A, n);
- cout << "\nИскомое число: ";
- cin >> c;
- nElement = BinSearch(A, n, c);
- if (nElement != -1) cout << "Число " << c << " находится в ячейке с индексом: " << nElement << endl;
- else cout << "В массиве нет такого значения" << endl;
- _getch();
- return 0;
- }
- void generator(int* mass, int n) {
- for (int i = 0; i < n; i++)
- mass[i] = rand() % 101 - 50;
- }
- void print(int* mass, int n) {
- cout << '{';
- for (int i = 0; i < n; i++) {
- if (i != n-1)
- cout << mass[i] << ", ";
- else
- cout << mass[i];
- }
- cout << "}\n";
- }
- int linSearch(int* mass, int z, int y) {
- for (int i = 0; i < z; i++) {
- if (mass[i] == y) return i;
- }
- return -1;
- }
- void BubbleSort(int* mass, int n) {
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n - i - 1; j++) {
- if (mass[j] > mass[j + 1]) {
- int t = mass[j];
- mass[j] = mass[j + 1];
- mass[j + 1] = t;
- }
- }
- }
- }
- int BinSearch(int* mass, int n, int k) {
- int L = 0, R = n - 1, m;
- while (L < R) {
- m = (L + R) / 2;
- k > mass[m] ? L = m + 1 : R = m;
- }
- if (k == mass[L]) return L;
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement