Advertisement
StoneHaos

mod_n2_v18

Jan 26th, 2022
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. void Shell(int *a, int n, int d = 5) {
  9.     for (int t = ((d % 2 == 0) ? d + 1 : d); t > 0; t -= 2) {
  10.         for (int i = 1; i < n; i += t) {
  11.             for (int j = i; j - t >= 0 && a[j] < a[j - t]; j -= t)
  12.                 swap(a[j], a[j - t]);
  13.         }
  14.     }
  15. }
  16.  
  17. void out(int *a, int n) {
  18.     for (int i = 0; i < n; ++ i) {
  19.         cout << a[i] << " ";
  20.     }
  21.     cout << endl;
  22. }
  23.  
  24. int f1(int *a, int n, int b) {
  25.     for (int i = 0; i < n; ++ i) {
  26.         if (a[i] == b) return i;
  27.     }
  28.     return -1;
  29. }
  30.  
  31. int f2(int *a, int n, int Key) {
  32.     int l=0, r=n-1;
  33.     while(l!=r){
  34.         int m=(l+r)/2;
  35.         if(Key>a[m]) l=m+1;
  36.         else r=m;
  37.     }
  38.     if (a[l]=Key) return l;
  39.     else return -1;
  40.  
  41. }
  42.  
  43. const int n = 10;
  44. int a[n];
  45.  
  46. int main() {
  47.     srand(time(NULL));
  48.     for (int i = 0; i < n; ++ i)
  49.         a[i] = rand() % 100;
  50.     int x, y;
  51.     out(a, n);
  52.     cin >> x;
  53.     cout << f1(a, n, x) << endl;
  54.     Shell(a, n);
  55.     out(a, n);
  56.     cin >> y;
  57.     cout << f2(a, n, y) << endl;
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement