juinda

templateBS

Jun 14th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #include <iostream>
  2. #include<string>
  3. using namespace std;
  4. const int ARRAY_SIZE = 8;
  5. template<class T>
  6. void recBinSrch(const T a[], int first, int last,
  7. T key, bool& found, int& location);
  8. //Precondition: Type T must support = == and >
  9. //a[first] through a[last] are sorted in increasing order.
  10. //Postcondition:
  11. //if key is not one of the values a[first] through a[last],
  12. //then found == false;
  13. //otherwise a[location] == key and found == true.
  14.  
  15. template<class T>
  16. void ItrSearch(const T a[], int low, int high,
  17. T key, bool& found, int& location);
  18. //Precondition: Type T must support = == and >
  19. // a[first] through a[last] are sorted in increasing order.
  20. //Postcondition: if key is not one of the values a[first] through a[last],
  21. //then found == false; otherwise a[location] == key and found == true.
  22.  
  23. int main()
  24. {
  25. const int finalIndex = ARRAY_SIZE - 1;
  26.  
  27. int i;
  28. double a[] = { 3.3 , 6.6 , 8.8 , 9.9 , 11.1 , 15.1 , 16.1 , 20.2 };
  29. std::string b[] = { "aaa", "abc", "ahh", "bde", "bee", "ccc", "feg", "hee" };
  30. int c[] = { 15, 33, 45, 46, 49, 59, 70, 81 };
  31. //test int
  32. cout << "Array contains:\n";
  33. for (i = 0; i < ARRAY_SIZE; i++)
  34. cout << c[i] << " ";
  35. cout << endl;
  36. int key, location;
  37. bool found;
  38. cout << "Enter number to be located: ";
  39. cin >> key;
  40.  
  41. cout << "\nTesting Template Iterative Binary Search\n";
  42. ItrSearch(c, 0, finalIndex, key, found, location);
  43. if (found)
  44. cout << key << " is in index location "
  45. << location << endl;
  46. else
  47. cout << key << " is not in the array." << endl;
  48.  
  49.  
  50. cout << "\nTesting Template Recursive Binary Search\n";
  51. recBinSrch(c, 0, finalIndex, key, found, location);
  52. if (found)
  53. cout << key << " is in index location "
  54. << location << endl;
  55. else
  56. cout << key << " is not in the array." << endl;
  57.  
  58. //test string
  59. std::string keyString;
  60. cout << endl;
  61. cout << "Array contains:\n";
  62. for (i = 0; i < ARRAY_SIZE; i++)
  63. cout << b[i] << " ";
  64. cout << endl;
  65. cout << "Enter string to be located: ";
  66. cin >> keyString;
  67.  
  68. cout << "\nTesting Template Iterative Binary Search\n";
  69. ItrSearch(b, 0, finalIndex, keyString, found, location);
  70. if (found)
  71. cout << keyString << " is in index location "
  72. << location << endl;
  73. else
  74. cout << keyString << " is not in the array." << endl;
  75.  
  76.  
  77. cout << "\nTesting Template Recursive Binary Search\n";
  78. recBinSrch(b, 0, finalIndex, keyString, found, location);
  79. if (found)
  80. cout << keyString << " is in index location "
  81. << location << endl;
  82. else
  83. cout << keyString << " is not in the array." << endl;
  84. //test double
  85. double keydouble;
  86. cout << endl;
  87. cout << "Array contains:\n";
  88. for (i = 0; i < ARRAY_SIZE; i++)
  89. cout << a[i] << " ";
  90. cout << endl;
  91. cout << "Enter number to be located: ";
  92. cin >> keydouble;
  93.  
  94. cout << "\nTesting Template Iterative Binary Search\n";
  95. ItrSearch(a, 0, finalIndex, keydouble, found, location);
  96. if (found)
  97. cout << keydouble << " is in index location "
  98. << location << endl;
  99. else
  100. cout << keydouble << " is not in the array." << endl;
  101.  
  102.  
  103. cout << "\nTesting Template Recursive Binary Search\n";
  104. recBinSrch(a, 0, finalIndex, keydouble, found, location);
  105. if (found)
  106. cout << keydouble << " is in index location "
  107. << location << endl;
  108. else
  109. cout << keydouble << " is not in the array." << endl;
  110. return 0;
  111. }
  112.  
  113. template<class T>
  114. void recBinSrch(const T a[], int first, int last,
  115. T key, bool& found, int& location)
  116. {
  117. int mid;
  118. if (first > last)
  119. {
  120. found = false;
  121. }
  122. else
  123. {
  124. mid = (first + last) / 2;
  125. if (key == a[mid]) // T must support ==
  126. {
  127. found = true;
  128. location = mid;
  129. }
  130. else if (a[mid] > key) // T must support >
  131. {
  132. recBinSrch(a, first, mid - 1, key, found, location);
  133. }
  134. else if (key > a[mid]) // Changed to avoid requiring >
  135. {
  136. recBinSrch(a, mid + 1, last, key, found, location);
  137. }
  138. }
  139. }
  140.  
  141. template<class T>
  142. void ItrSearch(const T a[], int low, int high, T key, bool& found, int& location)
  143. {
  144. int first = low;
  145. int last = high;
  146.  
  147. int mid;
  148.  
  149. found = false;
  150. while ((first <= last) && !found)
  151. {
  152. mid = (first + last) / 2;
  153.  
  154. if (key == a[mid]) //T must support ==
  155. {
  156. found = true;
  157. location = mid;
  158. }
  159. else if (a[mid] > key) // T must support >
  160. last = mid - 1;
  161. else if (key > a[mid]) // Changed to avoid requiring >
  162. first = mid + 1;
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment