Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- #include <algorithm>
- using namespace std;
- void Shell(int *a, int n, int d = 5) {
- for (int t = ((d % 2 == 0) ? d + 1 : d); t > 0; t -= 2) {
- for (int i = 1; i < n; i += t) {
- for (int j = i; j - t >= 0 && a[j] < a[j - t]; j -= t)
- swap(a[j], a[j - t]);
- }
- }
- }
- void out(int *a, int n) {
- for (int i = 0; i < n; ++ i) {
- cout << a[i] << " ";
- }
- cout << endl;
- }
- int f1(int *a, int n, int b) {
- for (int i = 0; i < n; ++ i) {
- if (a[i] == b) return i;
- }
- return -1;
- }
- int f2(int *a, int n, int Key) {
- int l=0, r=n-1;
- while(l!=r){
- int m=(l+r)/2;
- if(Key>a[m]) l=m+1;
- else r=m;
- }
- if (a[l]=Key) return l;
- else return -1;
- }
- const int n = 10;
- int a[n];
- int main() {
- srand(time(NULL));
- for (int i = 0; i < n; ++ i)
- a[i] = rand() % 100;
- int x, y;
- out(a, n);
- cin >> x;
- cout << f1(a, n, x) << endl;
- Shell(a, n);
- out(a, n);
- cin >> y;
- cout << f2(a, n, y) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement