Advertisement
Guest User

Untitled

a guest
May 24th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. const int N=17;
  5. //интерполяционный поиск
  6. int InterpolSearch(int A[], int key)
  7. {
  8. int mid, left=0, right=N-1;
  9. while (A[left]<=key && A[right]>=key)
  10. {
  11. mid=left+((key-A[left])*(right-left))/(A[right]-A[left]);
  12. if (A[mid]<key) left=mid+1;
  13. else if (A[mid]>key) right=mid-1;
  14. else return mid;
  15. }
  16. if (A[left]==key) return left;
  17. else return -1;
  18. }
  19. //главная функция
  20. void main()
  21. {
  22. setlocale(LC_ALL,"Rus");
  23. int i, key;
  24. int A[N]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59};
  25. cout<<"Искомый элемент > "; cin>>key; //ввод ключа
  26. cout<<"Исходный массив: ";
  27. for (i=0; i<N; i++) cout<<A[i]<<" "; //вывод массива
  28. if (InterpolSearch(A, key)==-1) cout<<"\nЭлемент не найден";
  29. else cout<<"\nНомер элемента: "<<InterpolSearch(A, key)+1;
  30. system("pause>>void");
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement