Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. int linearSearch(std::vector<int> &vectorToSearch, int itemToTest);
  5. void bubbleSort(std::vector<int> &vectorToSort);
  6. int binarySearch(std::vector<int> &vectorToSearch, int numToTest);
  7.  
  8. int main() {
  9. std::vector<int> myVector;
  10. myVector.push_back(5);
  11. myVector.push_back(20);
  12. myVector.push_back(18);
  13. myVector.push_back(3);
  14. myVector.push_back(9);
  15. myVector.push_back(4);
  16. myVector.push_back(11);
  17. myVector.push_back(30);
  18. myVector.push_back(2);
  19. myVector.push_back(8);
  20. myVector.push_back(14);
  21. myVector.push_back(22);
  22. myVector.push_back(24);
  23. int idx = linearSearch(myVector, 14); // This should return the index of where the item was found
  24. std::cout << "Found item at index " << idx << std::endl;
  25. std::cout << "Before sorting: " << std::endl;
  26. for (int num : myVector) {
  27. std::cout << num << " ";
  28. }
  29. bubbleSort(myVector);
  30. std::cout << std::endl << "After sorting: " << std::endl;
  31. for (int num : myVector) {
  32. std::cout << num << " ";
  33. }
  34. idx = linearSearch(myVector, 14); // This should return the index of where the item was found
  35. std::cout << "Found item at index " << idx << std::endl;
  36. int x = 1;
  37. std::cin >> x;
  38. idx = binarySearch(myVector, 4); // Should return the index where the item was found
  39. std::cout << "Using binary search, we found 4 at index " << idx << std::endl;
  40. idx = linearSearch(myVector, 4); // This should return the index of where the item was found
  41. std::cout << "Using linear search, we found 4 at index " << idx << std::endl;
  42.  
  43. idx = binarySearch(myVector, 28); // Should return the index where the item was found
  44. std::cout << "Using binary search, we found 28 at index " << idx << std::endl;
  45. idx = linearSearch(myVector, 28); // This should return the index of where the item was found
  46. std::cout << "Using linear search, we found 28 at index " << idx << std::endl;
  47.  
  48. //x = 5 / x;
  49. //std::cout << x << std::endl;
  50. //std::cin >> x;
  51. }
  52.  
  53. void bubbleSort(std::vector<int> &vectorToSort) {
  54. // Save off the size so we don't have to compute it every time.
  55. int size = vectorToSort.size();
  56.  
  57. // Loop through the list twice to do bubble sort.
  58. // The inner loop swaps items in the vector if needs be.
  59. // The outer loop makes sure we run the swaps enough times to actually be sorted.
  60. for (int i = 0; i < size; i++) {
  61. for (int j = 0; j < size - 1; j++) {
  62. // Do we need to swap?
  63. if (vectorToSort[j] > vectorToSort[j + 1]) {
  64. // Yes, we do.
  65. int temp = vectorToSort[j + 1];
  66. vectorToSort[j + 1] = vectorToSort[j];
  67. vectorToSort[j] = temp;
  68. }
  69. }
  70. }
  71. }
  72.  
  73. // This should return the index of where the item was found
  74. int linearSearch(std::vector<int> &myVect, int numToTest) {
  75. // loop through the vector
  76. for (int i = 0; i < myVect.size(); i++) {
  77. // if we found the item we were looking for
  78. if (myVect[i] == numToTest) {
  79. // return the index
  80. return i;
  81. }
  82. }
  83. // If we looped through the entire vector and never returned, we never found the item.
  84. // so return -1 as an error condition.
  85. return -1;
  86. }
  87.  
  88. int binarySearch(std::vector<int> &myVect, int numToTest) {
  89. //if (myVect.size() < 1) {
  90. // return -1;
  91. //}
  92. bool done = false;
  93. int start = 0;
  94. int end = myVect.size();
  95. while (!done) {
  96. // Get midpoint.
  97. int midpoint = floor((start + end / 2));
  98.  
  99. // Special case ending (base case).
  100. if (end - start <= 2) {
  101. // check the remaining two items
  102. if (myVect[end] == numToTest) return end;
  103. if (myVect[start] == numToTest) return start;
  104. // if numToTest doesn't match either one, we never found the item.
  105. return -1;
  106. }
  107. // Check value at midpoint against numToTest.
  108. if (myVect[midpoint] == numToTest) {
  109. // if midpoint == numToTest return midpoint.
  110. return midpoint;
  111. }
  112. else if (myVect[midpoint] < numToTest) {
  113. // else if midpoint < numToTest
  114. // set start to midpoint
  115. start = midpoint;
  116. }
  117. else {
  118. // else
  119. // set end to midpoint
  120. end = midpoint;
  121. }
  122. }
  123. return -1;
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement