Guest User

Untitled

a guest
Jan 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include "vector"
  3. #include "xutility"
  4.  
  5.  
  6. using namespace std;
  7. //Swap elements in array
  8. void Swap(vector<int>& vHeap, vector<int>::size_type i, vector<int>::size_type j)
  9. {
  10. if (i == j)
  11. {
  12. return;
  13. }
  14.  
  15. int temp;
  16. temp = vHeap[i];
  17. vHeap[i] = vHeap[j];
  18. vHeap[j] = temp;
  19. }
  20. // search and compare the elements in array and subsequently swap them
  21. //to their rightful spot
  22. void Sift(vector<int>& vHeap, const vector<int>::size_type heapSize, const vector<int>::size_type siftNode)
  23. {
  24. vector<int>::size_type i, j;
  25. j = siftNode;
  26. do
  27. {
  28. i = j;
  29. if (((2*i +1)< heapSize) && vHeap[j] < vHeap[2*i +1])
  30. {
  31. j = 2 * i + 1;
  32. }
  33. if (((2 * i + 2) < heapSize) && vHeap[j] < vHeap[2 * i + 2])
  34. {
  35. j = 2 * i + 2;
  36. }
  37. Swap(vHeap, i, j);
  38. } while (i!= j);
  39. }
  40. void MakeInitialHeap(vector<int>& vHeap)
  41. {
  42. for (int i = vHeap.size() -1; i >= 0; --i)
  43. {
  44. Sift(vHeap, vHeap.size(), i);
  45. }
  46. }
  47. // Actual sorting of the array
  48. vector<int> HeapSort(vector<int>& vHeap)
  49. {
  50. vector<int> NewHeap;
  51. MakeInitialHeap(vHeap);
  52. for (vector<int>::size_type i = vHeap.size() - 1; i > 0; --i)
  53. {
  54. Swap(vHeap, i, 0);
  55. Sift(vHeap, i, 0);
  56. NewHeap.push_back(vHeap[i]);
  57. }
  58. return NewHeap;
  59. }
  60.  
  61. int IntSearch(vector<int>& Arr, int e)// Interpolation Search
  62. {
  63. int _end = Arr.size()-1;
  64. int _start = 0;
  65. int _pos = 0;
  66.  
  67. while (_start <= _end && e >= _start && e <= Arr[_end])
  68. {
  69. _pos = _start + (((_end - _start) / (Arr[_end] - Arr[_start]))* (e - Arr[_start]));
  70. if (Arr[_pos] == e)
  71. {
  72. return _pos;
  73. }
  74. if (e > Arr[_pos])
  75. {
  76. _start = _pos + 1;
  77. }
  78. else
  79. {
  80. _end = _pos - 1;
  81. }
  82. }
  83. return -1;
  84.  
  85.  
  86. }
  87.  
  88. int main()
  89. {
  90. vector<int> PrimeList = { 2,199, 3, 43, 47, 5, 7, 109, 113, 127, 11, 17, 31, 37,
  91. 41, 53, 59, 61, 67, 179, 181, 71, 73, 79, 83,
  92. 89, 97, 101, 103,19, 23, 29, 107, 131, 137,
  93. 139, 167, 173, 191, 149, 151, 157, 163, 193, 197,13 };//array of unsorted prime numbers up to 200.
  94. int x;
  95. char ch;
  96. HeapSort(PrimeList);
  97. for (vector<int>::size_type i = 0; i < PrimeList.size(); i++)
  98. {
  99. cout << PrimeList[i] << endl;
  100. }
  101. up:// Label, in case the user wants to search again.
  102. cout << "\nEnter the Prime Number you want to search for: ";
  103. cin >> x;
  104. int _result = IntSearch(PrimeList,x);
  105. if (_result == -1)
  106. cout << "Element not Found!" << endl;
  107. else if (_result != -1)
  108. {
  109. cout << "The number " << x << "was found at the " << _result << " place in the list." << endl;
  110. }
  111. cout << "Would you like to do another search? (y/n)" << endl;
  112. cin >> ch;
  113. if (ch == 'Y'||ch == 'y')
  114. {
  115. goto up;// go back to the label.
  116. }
  117.  
  118. return 0;
  119. }
Add Comment
Please, Sign In to add comment