Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. enum SearchType {
  8. LessThan = 0,
  9. LessThanEquals = 1,
  10. Equals = 2,
  11. GreaterThanEquals = 3,
  12. GreaterThan = 4
  13. };
  14.  
  15. enum SearchResult {
  16. NotFound = 0,
  17. FoundExact,
  18. FoundGreater,
  19. FoundLess
  20. };
  21.  
  22. /* This function returns the index of the element equal to greater than key */
  23. int SearchKey(const int * const items, const int n_items, const int ascending, const int key)
  24. {
  25. for(int i = (ascending? 0 : n_items-1); ascending?(i < n_items):(i>0); ascending? i++ : i--)
  26. {
  27. if (key <= items[i])
  28. {
  29. return i;
  30. }
  31. }
  32.  
  33. return -1;
  34. }
  35.  
  36.  
  37. SearchResult Search(const int * const items,
  38. const int n_items,
  39. const int ascending,
  40. const int key,
  41. const SearchType type,
  42. int* const index)
  43. {
  44. int start = ascending ? 0 : n_items-1;
  45. int end = ascending ? n_items-1 : 0;
  46.  
  47. *index = -1;
  48.  
  49. switch(type)
  50. {
  51. int tempIndex;
  52.  
  53. case LessThan:
  54. if(key <= items[start])
  55. {
  56. return NotFound;
  57. }
  58. else if(key > items[end])
  59. {
  60. *index = end;
  61. return FoundLess;
  62. }
  63. else
  64. {
  65. tempIndex = SearchKey(items, n_items, ascending, key);
  66. *index = (ascending) ? tempIndex - 1 : tempIndex + 1;
  67. return FoundLess;
  68. }
  69.  
  70. case LessThanEquals:
  71. if(key < items[start])
  72. {
  73. return NotFound;
  74. }
  75. else if(key == items[start])
  76. {
  77. *index = start;
  78. return FoundExact;
  79. }
  80. else if(key > items[end])
  81. {
  82. *index = end;
  83. return FoundLess;
  84. }
  85. else
  86. {
  87. tempIndex = SearchKey(items, n_items, ascending, key);
  88. if (key == items[tempIndex])
  89. {
  90. *index = tempIndex;
  91. return FoundExact;
  92. }
  93. else
  94. {
  95. *index = (ascending) ? tempIndex -1 : tempIndex + 1;
  96. return FoundLess;
  97. }
  98. }
  99.  
  100. case Equals:
  101. if (key > items[end] || key < items[start])
  102. {
  103. return NotFound;
  104. }
  105. else
  106. {
  107. tempIndex = SearchKey(items, n_items, ascending, key);
  108. if (key == items[tempIndex])
  109. {
  110. *index = tempIndex;
  111. return FoundExact;
  112. }
  113.  
  114. return NotFound;
  115. }
  116.  
  117. case GreaterThanEquals:
  118. if (key < items[start])
  119. {
  120. *index = start;
  121. return FoundGreater;
  122. }
  123. else if (key == items[start])
  124. {
  125. *index = start;
  126. return FoundExact;
  127. }
  128. else if (key > items[end])
  129. {
  130. return NotFound;
  131. }
  132. else if (key == items[end])
  133. {
  134. *index = end;
  135. return FoundExact;
  136. }
  137. else
  138. {
  139. tempIndex = SearchKey(items, n_items, ascending, key);
  140. *index = tempIndex;
  141. return (key == items[tempIndex])? FoundExact: FoundGreater;
  142. }
  143.  
  144. case GreaterThan:
  145. if (key < items[start])
  146. {
  147. *index = start;
  148. return FoundGreater;
  149. }
  150. else if (key >= items[end])
  151. {
  152. return NotFound;
  153. }
  154. else
  155. {
  156. tempIndex = SearchKey(items, n_items, ascending, key);
  157. *index = (key == items[tempIndex])? (ascending?tempIndex+1:tempIndex-1) : tempIndex;
  158. return FoundGreater;
  159. }
  160.  
  161. default:
  162. return NotFound;
  163.  
  164. }
  165. }
  166.  
  167. /* Function returns readable string for given enum SearchResult */
  168. const char* getSearchTypeName(SearchResult result)
  169. {
  170. switch (result)
  171. {
  172. case NotFound:
  173. return "NotFound";
  174.  
  175. case FoundExact:
  176. return "FoundExact";
  177.  
  178. case FoundGreater:
  179. return "FoundGreater";
  180.  
  181. case FoundLess:
  182. return "FoundLess";
  183. }
  184. }
  185.  
  186. /* Function returns readable string for given enum SearchType */
  187. const char* getTypeName(SearchType type)
  188. {
  189. switch(type)
  190. {
  191. case LessThan:
  192. return "LessThan";
  193.  
  194. case LessThanEquals:
  195. return "LessThanEquals";
  196.  
  197. case Equals:
  198. return "Equals";
  199.  
  200. case GreaterThanEquals:
  201. return "GreaterThanEquals";
  202.  
  203. case GreaterThan:
  204. return "GreaterThan";
  205. }
  206. }
  207.  
  208. void printArray(const int * const items, const int n_items)
  209. {
  210. int i;
  211. for(i=0; i<n_items;i++)
  212. {
  213. cout << items[i] << " ";
  214. }
  215. cout << endl;
  216. }
  217.  
  218.  
  219. void printOutput(int const key, const SearchType type, const SearchResult res, int * const index)
  220. {
  221. cout << key << " " << getTypeName(type) << " " << getSearchTypeName(res) << " " << (int)(*index) << endl;
  222. }
  223.  
  224. int main()
  225. {
  226. // Sample arrays for testing
  227. const int arr[] = {10, 8, 6, 4, 2, 0};
  228.  
  229. // search element and type
  230. int key = 2;
  231. SearchType type = Equals;
  232.  
  233. // If search result is NotFound, then index will be set to default value -1
  234. int default_index = -1;
  235. int* const index = &default_index;
  236.  
  237. // Input array length
  238. int arr_len = sizeof(arr)/sizeof(int);
  239.  
  240. // Array order - Ascending or descending
  241. bool is_ascending = (arr[0]<arr[1]) ? 1 : 0;
  242.  
  243. SearchResult res = Search(arr, arr_len, is_ascending, key, type, index);
  244. printOutput(key, type, res, index);
  245.  
  246. return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement