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 print(int *arr, int n) {
- for (int i = 0; i < n; ++ i)
- cout << arr[i] << " ";
- cout << endl;
- }
- int LS(int *arr, int n, int e) {
- int res = -1;
- for (int i = 0; i < n; ++ i) {
- if (arr[i] == e) res = i;
- }
- return res;
- }
- 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]);
- }
- }
- }
- int IS(int *A, int N, int key) {
- int m, l = 0, r = N - 1;
- while (A[l] <= key && A[r] >= key) {
- m = l + ((key - A[l])*(r - l)) / (A[r] - A[l]);
- if (A[m] < key) l = m + 1;
- else if (A[m] > key) r = m - 1;
- else {
- l = m;
- break;
- }
- }
- if (A[l] == key) return l; else return -1;
- }
- int main(void) {
- srand(time(NULL));
- int n;
- cin >> n;
- int *arr = (int*)malloc(n * sizeof(int));
- for (int i = 0; i < n; ++ i) {
- arr[i] = rand() % 100;
- }
- print(arr, n);
- cout << "> ";
- int a;
- cin >> a;
- cout << LS(arr, n, a) << endl << endl;
- Shell(arr, n);
- print(arr, n);
- cout << "> ";
- cin >> a;
- cout << IS(arr, n, a) << endl << endl;
- free(arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement